Added support for meta commands for header
This commit is contained in:
parent
ea65c1ed2d
commit
cc620c9700
|
@ -73,31 +73,32 @@
|
||||||
(defun md4tj-convert-line-to-html (line state)
|
(defun md4tj-convert-line-to-html (line state)
|
||||||
"Process LINE with STATE and return html."
|
"Process LINE with STATE and return html."
|
||||||
(let ((cleanline (md4tj-clean-multiline line)))
|
(let ((cleanline (md4tj-clean-multiline line)))
|
||||||
(concat
|
(if (string-match "^@@" line) "" ;; Don't process if @@ statement
|
||||||
;; Beginning of multiline block
|
(concat
|
||||||
(cond ((eq state 'beginul) "<ul>\n<li>")
|
;; Beginning of multiline block
|
||||||
((eq state 'beginol) "<ol>\n<li>")
|
(cond ((eq state 'beginul) "<ul>\n<li>")
|
||||||
((eq state 'begincode) "<pre>\n<code>")
|
((eq state 'beginol) "<ol>\n<li>")
|
||||||
((eq state 'ul) "<li>")
|
((eq state 'begincode) "<pre>\n<code>")
|
||||||
((eq state 'ol) "<li>")
|
((eq state 'ul) "<li>")
|
||||||
((eq state 'code) "")
|
((eq state 'ol) "<li>")
|
||||||
((eq state 'endul) "</ul>\n")
|
((eq state 'code) "")
|
||||||
((eq state 'endol) "</ol>\n")
|
((eq state 'endul) "</ul>\n")
|
||||||
((eq state 'endcode) "</code>\n</pre>\n")
|
((eq state 'endol) "</ol>\n")
|
||||||
(t ""))
|
((eq state 'endcode) "</code>\n</pre>\n")
|
||||||
|
(t ""))
|
||||||
|
|
||||||
;; Body
|
;; Body
|
||||||
(cond ((or (eq state 'code) (eq state 'begincode)) (md4tj-clean-code-for-html cleanline))
|
(cond ((or (eq state 'code) (eq state 'begincode)) (md4tj-clean-code-for-html cleanline))
|
||||||
((string-match "^#+ " cleanline) (md4tj-process-header (md4tj-process-line cleanline)))
|
((string-match "^#+ " cleanline) (md4tj-process-header (md4tj-process-line cleanline)))
|
||||||
((string= "---" cleanline) "<hr>") ;; horizontal line
|
((string= "---" cleanline) "<hr>") ;; horizontal line
|
||||||
((= (length cleanline) 0) "<br/>") ;; blank line
|
((= (length cleanline) 0) "<br/>") ;; blank line
|
||||||
(t (md4tj-process-paragraph (md4tj-process-line cleanline))))
|
(t (md4tj-process-paragraph (md4tj-process-line cleanline))))
|
||||||
|
|
||||||
;; End of multiline block
|
;; End of multiline block
|
||||||
(cond ((or (eq state 'ul) (eq state 'beginul)) "</li>")
|
(cond ((or (eq state 'ul) (eq state 'beginul)) "</li>")
|
||||||
((or (eq state 'ol) (eq state 'beginol)) "</li>")
|
((or (eq state 'ol) (eq state 'beginol)) "</li>")
|
||||||
((eq state 'code) "")
|
((eq state 'code) "")
|
||||||
(t "")))))
|
(t ""))))))
|
||||||
|
|
||||||
(defun md4tj-next-state (currline prevstate)
|
(defun md4tj-next-state (currline prevstate)
|
||||||
"Return the state based on CURRLINE and PREVSTATE."
|
"Return the state based on CURRLINE and PREVSTATE."
|
||||||
|
@ -112,6 +113,11 @@
|
||||||
((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
|
((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
|
||||||
(t 'normal)))
|
(t 'normal)))
|
||||||
|
|
||||||
|
(defun md4tj-begin ()
|
||||||
|
"Insert beginning code for all html."
|
||||||
|
(concat "<!DOCTYPE html>\n"
|
||||||
|
(md4tj-begin-tag "html" (list (list "lang" "en-us"))) "\n"))
|
||||||
|
|
||||||
(defun md4tj-finalize (state)
|
(defun md4tj-finalize (state)
|
||||||
"Finalizes HTML document by inserting missing end tags based on STATE."
|
"Finalizes HTML document by inserting missing end tags based on STATE."
|
||||||
(concat
|
(concat
|
||||||
|
@ -122,6 +128,37 @@
|
||||||
"</body>\n"
|
"</body>\n"
|
||||||
"</html>"))
|
"</html>"))
|
||||||
|
|
||||||
|
(defun md4tj-find-metas ()
|
||||||
|
"Return all lines starting with @@ as list of split strings."
|
||||||
|
(save-excursion
|
||||||
|
(let ((meta-list nil))
|
||||||
|
(goto-char (point-min))
|
||||||
|
(while (re-search-forward "^@@" nil t)
|
||||||
|
(setq meta-list (cons (split-string (getline)) meta-list)))
|
||||||
|
meta-list)))
|
||||||
|
|
||||||
|
(defun md4tj-list-to-tuple-list (list)
|
||||||
|
"Convert LIST to tuple list."
|
||||||
|
(let ((tuples nil))
|
||||||
|
(dolist (i (number-sequence 0 (- (length list) 1) 2))
|
||||||
|
(setq tuples (cons (cons (nth i list) (list (nth (+ i 1) list))) tuples)))
|
||||||
|
(reverse tuples)))
|
||||||
|
|
||||||
|
(defun md4tj-meta-to-html (meta)
|
||||||
|
"Convert META to html."
|
||||||
|
(cond ((string= (nth 0 meta) "@@TITLE")
|
||||||
|
(concat "<title>" (mapconcat 'identity (cdr meta) " ") "</title>\n"))
|
||||||
|
((string= (nth 0 meta) "@@META")
|
||||||
|
(concat (md4tj-begin-tag "meta" (md4tj-list-to-tuple-list (cdr meta)))))))
|
||||||
|
|
||||||
|
(defun md4tj-head ()
|
||||||
|
"Return text for head element on current buffer."
|
||||||
|
(concat
|
||||||
|
(md4tj-begin-tag "head") "\n"
|
||||||
|
(mapconcat 'md4tj-meta-to-html (md4tj-find-metas) "\n")
|
||||||
|
"\n"
|
||||||
|
(md4tj-end-tag "head") "\n"))
|
||||||
|
|
||||||
(defun md4tj-parse (mdfile outfile)
|
(defun md4tj-parse (mdfile outfile)
|
||||||
"Entry point to parse MDFILE and output to OUTFILE."
|
"Entry point to parse MDFILE and output to OUTFILE."
|
||||||
(let ((inbuf (generate-new-buffer " in"))
|
(let ((inbuf (generate-new-buffer " in"))
|
||||||
|
@ -132,7 +169,9 @@
|
||||||
(insert-file-contents mdfile)
|
(insert-file-contents mdfile)
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(set-buffer outbuf)
|
(set-buffer outbuf)
|
||||||
(insert "<body>\n")
|
(insert (md4tj-begin))
|
||||||
|
(insert (with-current-buffer inbuf (md4tj-head)))
|
||||||
|
(insert (md4tj-begin-tag "body"))
|
||||||
(while (with-current-buffer inbuf (< (point) (point-max)))
|
(while (with-current-buffer inbuf (< (point) (point-max)))
|
||||||
(setq line (with-current-buffer inbuf (getline)))
|
(setq line (with-current-buffer inbuf (getline)))
|
||||||
(setq state (md4tj-next-state line state))
|
(setq state (md4tj-next-state line state))
|
||||||
|
|
|
@ -1,4 +1,14 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en-us">
|
||||||
|
<head>
|
||||||
|
<title>Test webpage</title>
|
||||||
|
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1.0">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
|
|
||||||
<h1>Hello <code>world</code>!</h1>
|
<h1>Hello <code>world</code>!</h1>
|
||||||
<h2>Hello world!</h2>
|
<h2>Hello world!</h2>
|
||||||
<h3>Hello world!</h3>
|
<h3>Hello world!</h3>
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
@@META charset UTF-8
|
||||||
|
@@META name viewport content width=device-width,initial-scale=1.0
|
||||||
|
@@TITLE Test webpage
|
||||||
# Hello `world`!
|
# Hello `world`!
|
||||||
## Hello world!
|
## Hello world!
|
||||||
### Hello world!
|
### Hello world!
|
||||||
|
|
Loading…
Reference in New Issue