Added support for meta commands for header

This commit is contained in:
j4nk 2022-12-29 13:41:39 -05:00
parent ea65c1ed2d
commit cc620c9700
3 changed files with 78 additions and 26 deletions

View File

@ -73,31 +73,32 @@
(defun md4tj-convert-line-to-html (line state)
"Process LINE with STATE and return html."
(let ((cleanline (md4tj-clean-multiline line)))
(concat
;; Beginning of multiline block
(cond ((eq state 'beginul) "<ul>\n<li>")
((eq state 'beginol) "<ol>\n<li>")
((eq state 'begincode) "<pre>\n<code>")
((eq state 'ul) "<li>")
((eq state 'ol) "<li>")
((eq state 'code) "")
((eq state 'endul) "</ul>\n")
((eq state 'endol) "</ol>\n")
((eq state 'endcode) "</code>\n</pre>\n")
(t ""))
;; Body
(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= "---" cleanline) "<hr>") ;; horizontal line
((= (length cleanline) 0) "<br/>") ;; blank line
(t (md4tj-process-paragraph (md4tj-process-line cleanline))))
;; End of multiline block
(cond ((or (eq state 'ul) (eq state 'beginul)) "</li>")
((or (eq state 'ol) (eq state 'beginol)) "</li>")
((eq state 'code) "")
(t "")))))
(if (string-match "^@@" line) "" ;; Don't process if @@ statement
(concat
;; Beginning of multiline block
(cond ((eq state 'beginul) "<ul>\n<li>")
((eq state 'beginol) "<ol>\n<li>")
((eq state 'begincode) "<pre>\n<code>")
((eq state 'ul) "<li>")
((eq state 'ol) "<li>")
((eq state 'code) "")
((eq state 'endul) "</ul>\n")
((eq state 'endol) "</ol>\n")
((eq state 'endcode) "</code>\n</pre>\n")
(t ""))
;; Body
(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= "---" cleanline) "<hr>") ;; horizontal line
((= (length cleanline) 0) "<br/>") ;; blank line
(t (md4tj-process-paragraph (md4tj-process-line cleanline))))
;; End of multiline block
(cond ((or (eq state 'ul) (eq state 'beginul)) "</li>")
((or (eq state 'ol) (eq state 'beginol)) "</li>")
((eq state 'code) "")
(t ""))))))
(defun md4tj-next-state (currline 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)
(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)
"Finalizes HTML document by inserting missing end tags based on STATE."
(concat
@ -122,6 +128,37 @@
"</body>\n"
"</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)
"Entry point to parse MDFILE and output to OUTFILE."
(let ((inbuf (generate-new-buffer " in"))
@ -132,7 +169,9 @@
(insert-file-contents mdfile)
(goto-char (point-min))
(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)))
(setq line (with-current-buffer inbuf (getline)))
(setq state (md4tj-next-state line state))

View File

@ -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>
<h1>Hello <code>world</code>!</h1>
<h2>Hello world!</h2>
<h3>Hello world!</h3>

View File

@ -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!