Added support for including files

This commit is contained in:
j4nk 2022-12-29 14:17:59 -05:00
parent cc620c9700
commit eacd91ea6f
4 changed files with 52 additions and 26 deletions

View File

@ -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) "<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 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) "<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."
@ -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

View File

@ -1,6 +1,7 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Test webpage</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
@ -13,6 +14,9 @@
<h2>Hello world!</h2>
<h3>Hello world!</h3>
<h4>Hello <strong><em>world</em></strong>!</h4>
<br/>
<h1>This was included from a separate file!</h1>
<br/>
<p><a href="https://example.com">Example website</a></p>
<p><img src="usbs.png" alt="Example image"></p>

View File

@ -6,6 +6,8 @@
### Hello world!
#### Hello ***world***!
@@INCLUDE test_file_include.md4tj
[Example website](https://example.com)
![Example image](usbs.png)

1
test_file_include.md4tj Normal file
View File

@ -0,0 +1 @@
# This was included from a separate file!