Allow ending and beginning multiline block in same block

This commit is contained in:
j4nk 2023-01-04 13:35:28 -05:00
parent 87c53d901e
commit 9301c73095
3 changed files with 74 additions and 43 deletions

View File

@ -93,43 +93,54 @@
;; Otherwise, process as normal ;; Otherwise, process as normal
(t (t
(concat (concat
;; Beginning of multiline block ;; Beginning multiline block/ending prev multiline block
(cond ((eq state 'beginul) "<ul>\n<li>") (mapconcat #'md4tj-state-to-html state "\n")
((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 ;; Body
(cond ((or (eq state 'code) (eq state 'begincode)) (md4tj-clean-code-for-html cleanline)) (cond ((or (eq (nth 1 state) 'code) (eq (nth 1 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 (nth 1 state) 'ul) (eq (nth 1 state) 'beginul)) "</li>")
((or (eq state 'ol) (eq state 'beginol)) "</li>") ((or (eq (nth 1 state) 'ol) (eq (nth 1 state) 'beginol)) "</li>")
((eq state 'code) "") ((eq (nth 1 state) 'code) "")
(t ""))))))) (t "")))))))
(defun md4tj-state-to-html (state)
"Convert STATE to html."
(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 "")))
(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."
(cond ((and (string-match "^- " currline) (not (or (eq prevstate 'beginul) (eq prevstate 'ul))) 'beginul)) (list
((and (string-match "^- " currline) (or (eq prevstate 'beginul) (eq prevstate 'ul)) 'ul)) ;; End state
((and (not (string-match "^- " currline)) (or (eq prevstate 'ul) (eq prevstate 'beginul))) 'endul) (cond ((and (string-match "^- " currline) (or (eq prevstate 'ol) (eq prevstate 'beginol))) 'endol)
((and (string-match "^[0-9]+\\. " currline) (not (or (eq prevstate 'beginol) (eq prevstate 'ol))) 'beginol)) ((and (string-match "^[0-9]+\\. " currline) (or (eq prevstate 'ul) (eq prevstate 'beginul))) 'endul)
((and (string-match "^[0-9]+\\. " currline) (or (eq prevstate 'beginol) (eq prevstate 'ol)) 'ol)) ((and (not (string-match "^- " currline)) (or (eq prevstate 'beginul) (eq prevstate 'ul))) 'endul)
((and (not (string-match "^[0-9]+\\. " currline)) (or (eq prevstate 'ol) (eq prevstate 'beginol))) 'endol) ((and (not (string-match "[0-9]+\\. " currline)) (or (eq prevstate 'beginol) (eq prevstate 'ol))) 'endul)
((and (string-match "^```" currline) (not (or (eq prevstate 'begincode) (eq prevstate 'code))) 'begincode)) ((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
((and (not (string-match "```$" currline)) (or (eq prevstate 'begincode) (eq prevstate 'code)) 'code)) (t 'nothing))
((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
(t 'normal))) ;; Begin state (or next line's prevstate)
(cond ((and (string-match "^- " currline) (not (or (eq prevstate 'beginul) (eq prevstate 'ul))) 'beginul))
((and (string-match "^- " currline) (or (eq prevstate 'beginul) (eq prevstate 'ul)) 'ul))
((and (string-match "^[0-9]+\\. " currline) (not (or (eq prevstate 'beginol) (eq prevstate 'ol))) 'beginol))
((and (string-match "^[0-9]+\\. " currline) (or (eq prevstate 'beginol) (eq prevstate 'ol)) 'ol))
((and (string-match "^```" currline) (not (or (eq prevstate 'begincode) (eq prevstate 'code))) 'begincode))
((and (not (string-match "```$" currline)) (or (eq prevstate 'begincode) (eq prevstate 'code)) 'code))
(t 'normal))))
(defun md4tj-begin () (defun md4tj-begin ()
"Insert beginning code for all html." "Insert beginning code for all html."
@ -186,7 +197,7 @@
"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"))
(outbuf (generate-new-buffer " out")) (outbuf (generate-new-buffer " out"))
(state 'normal) (fullstate (list 'nothing 'normal))
(line nil)) (line nil))
(set-buffer inbuf) (set-buffer inbuf)
(insert-file-contents mdfile) (insert-file-contents mdfile)
@ -197,12 +208,12 @@
(insert (md4tj-begin-tag "body")) (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 fullstate (md4tj-next-state line (nth 1 fullstate)))
;; Insert next line(s) into output file ;; Insert next line(s) into output file
(insert (concat (md4tj-convert-line-to-html line state) "\n")) (insert (concat (md4tj-convert-line-to-html line fullstate) "\n"))
;; Advance input file by a line ;; Advance input file by a line
(with-current-buffer inbuf (forward-line))) (with-current-buffer inbuf (forward-line)))
(insert (md4tj-finalize state)) (insert (md4tj-finalize (nth 1 fullstate)))
;; Write outbuf to outfile ;; Write outbuf to outfile
(write-region nil nil outfile nil))) (write-region nil nil outfile nil)))
@ -211,13 +222,13 @@
(with-temp-buffer (with-temp-buffer
(let ((acc nil) (let ((acc nil)
(line nil) (line nil)
(state 'normal)) (fullstate (list 'nothing 'normal)))
(insert-file-contents mdfile) (insert-file-contents mdfile)
(goto-char (point-min)) (goto-char (point-min))
(while (< (point) (point-max)) (while (< (point) (point-max))
(setq line (getline)) (setq line (getline))
(setq state (md4tj-next-state line state)) (setq fullstate (md4tj-next-state line (nth 1 fullstate)))
(setq acc (concat acc (md4tj-convert-line-to-html line state) "\n")) (setq acc (concat acc (md4tj-convert-line-to-html line fullstate) "\n"))
(forward-line)) (forward-line))
acc))) acc)))
(provide 'md4tj_parse) (provide 'md4tj_parse)

View File

@ -13,40 +13,63 @@
<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>
<h4>Hello <strong><em>world</em></strong>!</h4> <h4>Hello <strong><em>world</em></strong>!</h4>
<br/>
<h1>This was included from a separate file!</h1> <h1>This was included from a separate file!</h1>
<br/>
<p><a href="https://example.com">Example website</a></p> <p><a href="https://example.com">Example website</a></p>
<p><img src="usbs.png" alt="Example image"></p> <p><img src="usbs.png" alt="Example image"></p>
<p><video src="test_video.webm" type="video/webm" controls="true">Example video</video></p> <p><video src="test_video.webm" type="video/webm" controls="true">Example video</video></p>
<br/>
<ul> <ul>
<li><p>unordered <s>item 1</s></p></li> <li><p>unordered <s>item 1</s></p></li>
<li><p>unordered <mark>item 2</mark></p></li> <li><p>unordered <mark>item 2</mark></p></li>
<li><p>unordered item 3</p></li> <li><p>unordered item 3</p></li>
</ul> </ul>
<br/>
<ol> <ol>
<li><p>ordered item 1</p></li> <li><p>ordered item 1</p></li>
</ul>
<li><p>ordered item 2</p></li> <li><p>ordered item 2</p></li>
</ol> </ol>
<br/>
<ul>
<li><p>unordered item after ordered 1</p></li>
<li><p>unordered item after ordered 2</p></li>
</ul>
<pre> <pre>
<code> <code>
#include &lt;stdio.h&gt; #include &lt;stdio.h&gt;
int main() { int main() {
printf("%s\n", "Hello World!"); printf("%s\n", "Hello World!");
return 0; return 0;
} }
</code> </code>
</pre> </pre>
<br/> <br/>
<p><a href="https://crawl.develz.org">Dungeon Crawl Stone Soup</a> - My favorite FOSS roguelike (Yes, I know the greatest roguelike of all time is NetHack, I don't care).</p> <p><a href="https://crawl.develz.org">Dungeon Crawl Stone Soup</a> - My favorite FOSS roguelike (Yes, I know the greatest roguelike of all time is NetHack, I don't care).</p>
Last updated: Wed Jan 4 12:38:04 2023 Last updated: Wed Jan 4 13:32:42 2023
</body> </body>
</html> </html>

View File

@ -6,20 +6,17 @@
## Hello world! ## Hello world!
### Hello world! ### Hello world!
#### Hello ***world***! #### Hello ***world***!
@@INCLUDE test_file_include.md4tj @@INCLUDE test_file_include.md4tj
[Example website](https://example.com) [Example website](https://example.com)
![Example image](usbs.png) ![Example image](usbs.png)
!![Example video](test_video.webm) !![Example video](test_video.webm)
- unordered ~~item 1~~ - unordered ~~item 1~~
- unordered ==item 2== - unordered ==item 2==
- unordered item 3 - unordered item 3
1. ordered item 1 1. ordered item 1
2. ordered item 2 2. ordered item 2
- unordered item after ordered 1
- unordered item after ordered 2
``` ```
#include <stdio.h> #include <stdio.h>
int main() { int main() {