From cc620c97000c66623279a3579fa484e76420107c Mon Sep 17 00:00:00 2001 From: j4nk Date: Thu, 29 Dec 2022 13:41:39 -0500 Subject: [PATCH] Added support for meta commands for header --- md4tj_parse.el | 91 +++++++++++++++++++++++++++++++++++-------------- test_file.html | 10 ++++++ test_file.md4tj | 3 ++ 3 files changed, 78 insertions(+), 26 deletions(-) diff --git a/md4tj_parse.el b/md4tj_parse.el index 982ee6c..eb3a668 100644 --- a/md4tj_parse.el +++ b/md4tj_parse.el @@ -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) "\n") - ((eq state 'endol) "\n") - ((eq state 'endcode) "\n\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) "
") ;; horizontal line - ((= (length cleanline) 0) "
") ;; blank line - (t (md4tj-process-paragraph (md4tj-process-line cleanline)))) - - ;; End of multiline block - (cond ((or (eq state 'ul) (eq state 'beginul)) "") - ((or (eq state 'ol) (eq state 'beginol)) "") - ((eq state 'code) "") - (t ""))))) + (if (string-match "^@@" line) "" ;; Don't process if @@ statement + (concat + ;; Beginning of multiline block + (cond ((eq state 'beginul) "\n") + ((eq state 'endol) "\n") + ((eq state 'endcode) "\n\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) "
") ;; horizontal line + ((= (length cleanline) 0) "
") ;; blank line + (t (md4tj-process-paragraph (md4tj-process-line cleanline)))) + + ;; End of multiline block + (cond ((or (eq state 'ul) (eq state 'beginul)) "") + ((or (eq state 'ol) (eq state 'beginol)) "") + ((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 "\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 @@ "\n" "")) +(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 "" (mapconcat 'identity (cdr meta) " ") "\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 "\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)) diff --git a/test_file.html b/test_file.html index 878c267..20a06f4 100644 --- a/test_file.html +++ b/test_file.html @@ -1,4 +1,14 @@ + + + +Test webpage + + + + + +

Hello world!

Hello world!

Hello world!

diff --git a/test_file.md4tj b/test_file.md4tj index 69620d7..b1eafa4 100644 --- a/test_file.md4tj +++ b/test_file.md4tj @@ -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!