From eacd91ea6f2e7040f85c3b4d0366109bf1e611d6 Mon Sep 17 00:00:00 2001 From: j4nk Date: Thu, 29 Dec 2022 14:17:59 -0500 Subject: [PATCH] Added support for including files --- md4tj_parse.el | 71 ++++++++++++++++++++++++++--------------- test_file.html | 4 +++ test_file.md4tj | 2 ++ test_file_include.md4tj | 1 + 4 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 test_file_include.md4tj diff --git a/md4tj_parse.el b/md4tj_parse.el index eb3a668..28f4d30 100644 --- a/md4tj_parse.el +++ b/md4tj_parse.el @@ -73,32 +73,37 @@ (defun md4tj-convert-line-to-html (line state) "Process LINE with STATE and return html." (let ((cleanline (md4tj-clean-multiline line))) - (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 "")))))) + ;; If this is a signal to include another file + (cond ((string-match "^@@INCLUDE" line) (md4tj-parse-to-string (nth 1 (split-string line)))) + ;; If this is some other signal, ignore + ((string-match "^@@" line) "") + ;; Otherwise, process as normal + (t + (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." @@ -183,5 +188,19 @@ ;; Write outbuf to outfile (write-region nil nil outfile nil))) +(defun md4tj-parse-to-string (mdfile) + "Parse MDFILE, return conversion to HTML as string." + (with-temp-buffer + (let ((acc nil) + (line nil) + (state 'normal)) + (insert-file-contents mdfile) + (goto-char (point-min)) + (while (< (point) (point-max)) + (setq line (getline)) + (setq state (md4tj-next-state line state)) + (setq acc (concat acc (md4tj-convert-line-to-html line state) "\n")) + (forward-line)) + acc))) (provide 'md4tj_parse) ;;; md4tj_parse.el ends here diff --git a/test_file.html b/test_file.html index 20a06f4..2bb18f0 100644 --- a/test_file.html +++ b/test_file.html @@ -1,6 +1,7 @@ + Test webpage @@ -13,6 +14,9 @@

Hello world!

Hello world!

Hello world!

+
+

This was included from a separate file!

+

Example website

Example image

diff --git a/test_file.md4tj b/test_file.md4tj index b1eafa4..6ff491d 100644 --- a/test_file.md4tj +++ b/test_file.md4tj @@ -6,6 +6,8 @@ ### Hello world! #### Hello ***world***! +@@INCLUDE test_file_include.md4tj + [Example website](https://example.com) ![Example image](usbs.png) diff --git a/test_file_include.md4tj b/test_file_include.md4tj new file mode 100644 index 0000000..a20c8a6 --- /dev/null +++ b/test_file_include.md4tj @@ -0,0 +1 @@ +# This was included from a separate file! \ No newline at end of file