HTML、XHTML 的 DOCTYPE、html、meta、script .. 等等, 在 HTML5 有很大幅的簡化, 此篇文章只是紀錄如何快速將這些語法, 轉換 / 取代 成 HTML5 簡化的寫法.
轉換的 Shell script 指令如下:
# doctype
find . -regex ".*\.\(html\|py\)$" -type f -exec sed -i "s/<\!DOCTYPE\s\+html[^>]*>/<\!DOCTYPE html>/gi" {} \;# meta charset
find . -regex ".*\.\(html\|py\)$" -type f -exec sed -i "s/<meta[^>]*content=[\"'][^\"']*utf-8[\"'][^>]*>/<meta charset=\"utf-8\">/gi" {} \;# script text/javascript
find . -regex ".*\.\(html\|py\)$" -type f -exec sed -i "s/\(<script[^>]*\)\(\stype=[\"']text\/javascript[\"']\)\(\s\?[^>]*>\)/\1\3/gi" {} \;# style text/css
find . -regex ".*\.\(html\|py\)$" -type f -exec sed -i "s/\(<style[^>]*\)\(\stype=[\"']text\/css[\"']\)\(\s\?[^>]*>\)/\1\3/gi" {} \;# html xmlns
find . -regex ".*\.\(html\|py\)$" -type f -exec sed -i "s/\(<html[^>]*\)\(\sxmlns=[\"'][^\"']*[\"']\)\(\s\?[^>]*>\)/\1\3/gi" {} \;# html xml:lang
find . -regex ".*\.\(html\|py\)$" -type f -exec sed -i "s/\(<html[^>]*\)\(\sxml:lang=[\"'][^\"']*[\"']\)\(\s\?[^>]*>\)/\1\3/gi" {} \;
Script 轉換範例
上述的 Script, 會做下述的轉換:
- DOCTYPE
- 將 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- 轉換成 <!DOCTYPE html>
- html
- 將 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- 轉換成 <html lang="en">
- meta
- 將 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- 轉換成 <meta charset="utf-8">
- script
- 將 <script type="text/javascript">...</script>
- 轉換成 <script>...</script>
- script
- 將 <script type="text/javascript" src="foo.js">...</script>
- 轉換成 <script src="foo.js">...</script>
- style
- 將 <style type="text/css">...</style>
- 轉換成 <style>...</style>
若是 php / ruby 等等, 只需要將 "(html\|py\)" 修改/加上 即可.