Posted By

rareyman on 02/12/10


Tagged

textmate html perl


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

hsousa
brandonjp
adrianrf
kandarv


FHTML (for TextMate)


 / Published in: Perl
 

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

Report this snippet  

You need to login to post a comment.