FHTML (for TextMate)


/ Published in: Perl
Save to your folder(s)

I modified this excellent Perl script, created by John Watson (http://www.watson-net.com) to be used with TextMate to reformat your HTML for easier readability. Here's how to use it:

1. In TextMate > Bundles > Bundle Editor > Show Bundle Editor
2. Create a New Command (I call it "Reformat HTML")
3. On right pane: **Save: Nothing** : **Command: (insert code)**
4. Bottom right pane: **Input: Selected Text or Document**
5. Bottom right pane: **Output: Replace Selected Text**
6. Settings: Key Equivalent: (whatever you want, I use: Command-Option-Shift-F which matches old school BBEdit)
7. Scope Selector: text.html


Copy this code and paste it in your HTML
  1. #!/usr/bin/perl
  2. #
  3. # FHTML.PL (for TextMate)
  4. # Copyright (C) 1997 John Watson
  5. #
  6. # modified for TextMate by Ross A. Reyman - 12/2005
  7. #
  8. # -----About-----
  9. # This Script formats and indents html source code.
  10. # It makes code generated easier to read for humans.
  11. #
  12. # The latest copy of this script and documentation can be obtained from
  13. # http://www.watson-net.com/
  14.  
  15. # tab settings | how many spaces per tab? Use tabs?
  16. #
  17. $amount = 2;
  18. $usetabs = 1;
  19.  
  20. # These are the tags that will be formatted. Comment out to taste!
  21. #
  22. $tags = "<!DOCTYPE|<html|</html|<body|</body|<head|</head|<title|<meta|<style|</style";
  23. $tags .= "<script|</script";
  24. $tags .= "|<isindex|<link";
  25. $tags .= "|<table|<tr|<th|<td";
  26. #$tags .= "|</tr|</th|</td";
  27. $tags .= "|</table|</tr";
  28. $tags .= "|<caption";
  29. $tags .= "|<style|</style";
  30. $tags .= "|<thead|</thead|<tbody|</tbody";
  31. $tags .= "|<p";
  32. $tags .= "|<blockquote|</blockquote|<hr|<div|</div";
  33. $tags .= "|<img";
  34. $tags .= "|<h1|<h2|<h3|<h4|<h5|<h6";
  35. $tags .= "|<ul|</ul|<ol|</ol|<dl|</dl|<li|<dt|<dd|<dir|</dir|<menu|</menu";
  36. $tags .= "|<map|<area|</map";
  37. $tags .= "|<base";
  38. $tags .= "|<object|<applet|<param|</object|</applet|<embed|</embed";
  39. $tags .= "|<form|</form|<input|<select|<option|</select|<textarea";
  40. $tags .= "|<fieldset|</fieldset|<legend|<label";
  41.  
  42. # honor EE code as tags
  43. $tags .= "|{embed|{exp:|{/exp:";
  44.  
  45. # These tags get indented/unindented. Comment out to taste!
  46. #
  47. $tagindent = "<html|<body|<head";
  48. $tagindent .= "<style|<script";
  49. $tagindent .= "|<table|<th|<tr|<td";
  50. #$tagindent .= "|<h1|<h2|<h3|<h4|<h5|<h6";
  51. #$tagindent .= "|<p";
  52. $tagindent .= "|<div|<blockquote";
  53. $tagindent .= "|<select|<form|<option";
  54. $tagindent .= "|<ul|<ol|<dl|<dir|<menu|<map";
  55. $tagindent .= "|<fieldset|<legend|<label";
  56.  
  57. $tagunindent = "</html|</body|</head";
  58. $tagunindent .= "</style|</script";
  59. $tagunindent .= "|</table|</th|</tr|</td";
  60. #$tagunindent .= "|<h1|<h2|<h3|<h4|<h5|<h6";
  61. #$tagunindent .= "|</p";
  62. $tagunindent .= "|</div|</blockquote";
  63. $tagunindent .= "|</select|</form|</option";
  64. $tagunindent .= "|</ul|</ol|</dl|</dir|</menu|</map";
  65. $tagunindent .= "|</fieldset|</legend|</label";
  66.  
  67.  
  68. # pass 1 - get selection and plug into array
  69. @lines = ();
  70. @temp = ();
  71.  
  72. $x = 1;
  73. while (<>) {
  74. push @temp, $_;
  75. $x = $x + 1;
  76. }
  77. splitlines();
  78.  
  79. # pass 2 - remove tabs, clean up tags
  80. $SCRIPT = 0;
  81. $COMMENT = 0;
  82. $PRE = 0;
  83.  
  84. $temp = '';
  85. foreach(@lines) {
  86. $SCRIPT = 0 if ($line =~ m@(</script|%>|\?>)@ig);
  87. $COMMENT = 0 if ($line =~ m@(-->|</comment>|</style>)@ig);
  88. $PRE = 0 if ($line =~ m@</pre>@ig);
  89.  
  90. $line = $_;
  91.  
  92. $SCRIPT = 1 if ($line =~ m@(<script|<%|<\?php)@ig);
  93. $COMMENT = 1 if ($line =~ m@(<!--|<comment|<style)@ig);
  94. $PRE = 1 if ($line =~ m@<pre@ig);
  95.  
  96. if (!$SCRIPT && !$COMMENT && !$PRE) {
  97. # remove all tabs
  98. $line =~ s/\t//ig;
  99.  
  100. # remove spaces just before or after an angle bracket
  101. $line =~ s/<\ /</ig;
  102. $line =~ s/\ >/>/ig;
  103. if ($line =~ />$/) {
  104. $temp .= $line;
  105. } else {
  106. $temp .= $line." ";
  107. }
  108. } else {
  109. $temp .= "\n".$line."\n";
  110. }
  111. }
  112. push @temp, $temp;
  113. splitlines();
  114.  
  115. $SCRIPT = 0;
  116. $COMMENT = 0;
  117. $PRE = 0;
  118.  
  119. # pass 3 -
  120. foreach(@lines) {
  121. $SCRIPT = 0 if ($line =~ m@(</script|%>|\?>)@ig);
  122. $COMMENT = 0 if ($line =~ m@(-->|</comment>|</style>)@ig);
  123. $PRE = 0 if ($line =~ m@</pre>@ig);
  124.  
  125. $line = $_;
  126.  
  127. $SCRIPT = 1 if ($line =~ m@(<script|<%|<\?php)@ig);
  128. $COMMENT = 1 if ($line =~ m@(<!--|<comment|<style)@ig);
  129. $PRE = 1 if ($line =~ m@<pre@ig);
  130.  
  131. if (!$SCRIPT && !$COMMENT && !$PRE) {
  132. # remove extra whitespace
  133. $line =~ s/\ {2,}/\ /ig;
  134.  
  135. # put tags on new lines
  136. $line =~ s@($tags)@\n$1@ig;
  137. }
  138. push @temp, $line;
  139. }
  140. splitlines();
  141.  
  142. # pass 4 - indent tags (defined in $tagsindent)
  143. $indent = 0;
  144.  
  145. $SCRIPT = 0;
  146. $COMMENT = 0;
  147. $PRE = 0;
  148.  
  149. foreach (@lines) {
  150. $SCRIPT = 0 if ($line =~ m@(</script|%>|\?>)@ig);
  151. $COMMENT = 0 if ($line =~ m@(-->|</comment>|</style>)@ig);
  152. $PRE = 0 if ($line =~ m@</pre>@ig);
  153.  
  154. $line = $_;
  155.  
  156. $SCRIPT = 1 if ($line =~ m@(<script|<%|<\?php)@ig);
  157. $COMMENT = 1 if ($line =~ m@(<!--|<comment|<style)@ig);
  158. $PRE = 1 if ($line =~ m@<pre@ig);
  159.  
  160. $spaces = "";
  161. if (!$SCRIPT && !$COMMENT && !$PRE) {
  162. # remove trailing spaces
  163. $line =~ s@(\ $)@@ig;
  164.  
  165. $indent -= $line =~ s@($tagunindent)@$1@ig;
  166.  
  167. $spaces = "";
  168. for ($j=0; $j<$indent; $j++) {
  169. for ($k=0; $k<$amount; $k++) {
  170. if ($usetabs) {
  171. $spaces .= "\t";
  172. } else {
  173. $spaces .= " ";
  174. }
  175. }
  176. }
  177. }
  178.  
  179. push @temp, $spaces.$line;
  180.  
  181. if (!$SCRIPT && !$COMMENT && !$PRE) {
  182. $indent += $line =~ s/($tagindent)/$1/ig;
  183. }
  184. }
  185. splitlines();
  186.  
  187. # do it! (send back to TextMate)
  188. foreach (@lines) { print "$_\n"; }
  189.  
  190.  
  191. # sub-routines
  192. sub splitlines {
  193. @lines = ();
  194.  
  195. foreach(@temp) {
  196. $line = $_;
  197. if ($line eq "\n") {
  198. # This preserves blank lines in script and comments.
  199. push @lines, " ";
  200. } else {
  201. push @lines, split(/\n/, $line);
  202. }
  203. }
  204.  
  205. @temp = ();
  206. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.