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
(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 ""))
;; Beginning multiline block/ending prev multiline block
(mapconcat #'md4tj-state-to-html state "\n")
;; 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= "---" 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) "")
(cond ((or (eq (nth 1 state) 'ul) (eq (nth 1 state) 'beginul)) "</li>")
((or (eq (nth 1 state) 'ol) (eq (nth 1 state) 'beginol)) "</li>")
((eq (nth 1 state) 'code) "")
(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)
"Return the state based on CURRLINE and 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 (not (string-match "^- " currline)) (or (eq prevstate 'ul) (eq prevstate 'beginul))) 'endul)
((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 (not (string-match "^[0-9]+\\. " currline)) (or (eq prevstate 'ol) (eq prevstate 'beginol))) 'endol)
((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))
((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
(t 'normal)))
(list
;; End state
(cond ((and (string-match "^- " currline) (or (eq prevstate 'ol) (eq prevstate 'beginol))) 'endol)
((and (string-match "^[0-9]+\\. " currline) (or (eq prevstate 'ul) (eq prevstate 'beginul))) 'endul)
((and (not (string-match "^- " currline)) (or (eq prevstate 'beginul) (eq prevstate 'ul))) 'endul)
((and (not (string-match "[0-9]+\\. " currline)) (or (eq prevstate 'beginol) (eq prevstate 'ol))) 'endul)
((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
(t 'nothing))
;; 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 ()
"Insert beginning code for all html."
@ -186,7 +197,7 @@
"Entry point to parse MDFILE and output to OUTFILE."
(let ((inbuf (generate-new-buffer " in"))
(outbuf (generate-new-buffer " out"))
(state 'normal)
(fullstate (list 'nothing 'normal))
(line nil))
(set-buffer inbuf)
(insert-file-contents mdfile)
@ -197,12 +208,12 @@
(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))
(setq fullstate (md4tj-next-state line (nth 1 fullstate)))
;; 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
(with-current-buffer inbuf (forward-line)))
(insert (md4tj-finalize state))
(insert (md4tj-finalize (nth 1 fullstate)))
;; Write outbuf to outfile
(write-region nil nil outfile nil)))
@ -211,13 +222,13 @@
(with-temp-buffer
(let ((acc nil)
(line nil)
(state 'normal))
(fullstate (list 'nothing '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"))
(setq fullstate (md4tj-next-state line (nth 1 fullstate)))
(setq acc (concat acc (md4tj-convert-line-to-html line fullstate) "\n"))
(forward-line))
acc)))
(provide 'md4tj_parse)

View File

@ -13,40 +13,63 @@
<h1>Hello <code>world</code>!</h1>
<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>
<p><video src="test_video.webm" type="video/webm" controls="true">Example video</video></p>
<br/>
<ul>
<li><p>unordered <s>item 1</s></p></li>
<li><p>unordered <mark>item 2</mark></p></li>
<li><p>unordered item 3</p></li>
</ul>
<br/>
<ol>
<li><p>ordered item 1</p></li>
</ul>
<li><p>ordered item 2</p></li>
</ol>
<br/>
<ul>
<li><p>unordered item after ordered 1</p></li>
<li><p>unordered item after ordered 2</p></li>
</ul>
<pre>
<code>
#include &lt;stdio.h&gt;
int main() {
printf("%s\n", "Hello World!");
return 0;
}
</code>
</pre>
<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>
Last updated: Wed Jan 4 12:38:04 2023
Last updated: Wed Jan 4 13:32:42 2023
</body>
</html>

View File

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