Posted By

noah on 10/29/08


Tagged

regex html text filter conversion Converter elisp munge haml html2haml


Versions (?)

HAML conversion Emacs macros


 / Published in: Emacs Lisp
 

URL: http://github.com/nex3/haml/tree/master/bin/html2haml

Some utility functions I was working on just before I discovered html2haml.

  1. ;;;; HAML conversion macros
  2.  
  3. ;;;;; When the point is at the start of an attribute foo="bar" haml-ize it (or ruby-ize it, really) by changing that to
  4. ;;;;; :foo => "bar",
  5. (fset 'ygritte-haml-ize-html-attributes
  6. [?: ?\C-s ?= left ? right ?> ? ?\C-s ?\" ?\C-s right left ?, right])
  7.  
  8. ;;;;; Replace <td>foo</td> with %td foo
  9. (fset 'ygritte-haml-ize-table-row
  10. [?\C-s ?< ?t ?d left left backspace ?% right right right backspace ? ?\C-s ?< left ?\C-k ?\C-a tab down ?\C-a])
  11.  
  12. ;;;;; RHTML-to-HAML converter
  13. (defun ygritte-haml-from-rhtml ()
  14. "Convert simple RHTML markup to HAML."
  15. (interactive)
  16. (save-excursion
  17. ;get rid of HTML terminators
  18. (beginning-of-buffer)
  19. (replace-regexp "</[^>]+>" "")
  20.  
  21. ;get rid of RHTML terminators
  22. (beginning-of-buffer)
  23. (replace-regexp "%>" "")
  24.  
  25. ;convert opening RHTML tags
  26. (beginning-of-buffer)
  27. (replace-regexp "<%" "")
  28.  
  29. ;convert opening HTML tags
  30. (beginning-of-buffer)
  31. (replace-regexp "\\(^\s-*\\)<\\([^>]+\\)>" "\\1%\\2 ")
  32.  
  33. ;clean up RHTML declarations that are on the same line as a HAML tag
  34. (beginning-of-buffer)
  35. (replace-regexp "\\(%\\w+\\) =" "\\1=")
  36.  
  37. )
  38. )

Report this snippet  

You need to login to post a comment.