Created
January 24, 2010 17:49
-
-
Save mpasternacki/285335 to your computer and use it in GitHub Desktop.
Replace fragment of current buffer with htmlized file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'htmlize) | |
(defun jph/strip-htmlized-buffer () | |
"Strip htmlized buffer to <pre> tag, copying style attribute from <body> to <pre>." | |
(interactive) | |
(goto-char (point-min)) | |
(re-search-forward "<body\\( style=[^>]*\\)>") | |
(let ((style (match-string 1))) | |
(search-forward "<pre") | |
(insert style) | |
(delete-region (point-min) (line-beginning-position)) | |
(search-forward "</pre>") | |
(delete-region (point) (point-max)) | |
(insert "\n"))) | |
(defun jph/htmlize-fragment (prefix) | |
"Replace fragment of current buffer with htmlized file. | |
Text between comments <!-- htmlize filename --> and | |
<!-- end htmlize --> gets replaced with htmlized file | |
`filename' (only the <pre> tag). | |
If a negative prefix is given, text between comments is removed. | |
Only one pair of comments is processed (nearest one forward from | |
current point position). | |
" | |
(interactive "p") | |
(when (search-forward "<!-- htmlize " nil t) | |
(let ((source-file (thing-at-point 'filename))) | |
(beginning-of-line 2) | |
(let ((start (point))) | |
(search-forward "<!-- end htmlize ") | |
(beginning-of-line) | |
(delete-region start (point))) | |
(when (>= prefix 0) ; '-' prefix prevents inserting the htmlized text | |
(insert (with-current-buffer (find-file-noselect source-file) | |
(with-current-buffer (htmlize-buffer) | |
(jph/strip-htmlized-buffer) | |
(prog1 (buffer-string) | |
(kill-buffer))))))) | |
t)) | |
(defun jph/htmlize-all-fragments (prefix) | |
"Replace all indicated fragments of current buffer with htmlized files. | |
All pair of <!-- htmlize filename --> and <!-- end htmlize --> comments | |
are processed with `jph/htmlize-fragment'. If negative prefix is given, | |
inside of all such comment pairs is deleted." | |
(interactive "p") | |
(goto-char (point-min)) | |
(while (jph/htmlize-fragment prefix))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment