diff --git a/md4tj_parse.el b/md4tj_parse.el
index 87355db..03670ea 100644
--- a/md4tj_parse.el
+++ b/md4tj_parse.el
@@ -93,43 +93,54 @@
;; Otherwise, process as normal
(t
(concat
- ;; Beginning of multiline block
- (cond ((eq state 'beginul) "
\n- ")
- ((eq state 'beginol) "
\n- ")
- ((eq state 'begincode) "
\n")
- ((eq state 'ul) "- ")
- ((eq state 'ol) "
- ")
- ((eq state 'code) "")
- ((eq state 'endul) "
\n")
- ((eq state 'endol) "\n")
- ((eq state 'endcode) "\n\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) "
") ;; 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) "")
+ (cond ((or (eq (nth 1 state) 'ul) (eq (nth 1 state) 'beginul)) "")
+ ((or (eq (nth 1 state) 'ol) (eq (nth 1 state) 'beginol)) "")
+ ((eq (nth 1 state) 'code) "")
(t "")))))))
+(defun md4tj-state-to-html (state)
+ "Convert STATE to html."
+ (cond ((eq state 'beginul) "\n- ")
+ ((eq state 'beginol) "
\n- ")
+ ((eq state 'begincode) "
\n")
+ ((eq state 'ul) "- ")
+ ((eq state 'ol) "
- ")
+ ((eq state 'code) "")
+ ((eq state 'endul) "
\n")
+ ((eq state 'endol) "\n")
+ ((eq state 'endcode) "\n\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)
diff --git a/test_file.html b/test_file.html
index cbba4e8..86c9590 100644
--- a/test_file.html
+++ b/test_file.html
@@ -13,40 +13,63 @@
+
Hello world
!
+
Hello world!
+
Hello world!
+
Hello world!
-
+
This was included from a separate file!
-
+
Example website
+
+
-
+
unordered item 1
+
unordered item 2
+
unordered item 3
-
+
ordered item 1
+
+
ordered item 2
-
+
+
+
+
#include <stdio.h>
+
int main() {
+
printf("%s\n", "Hello World!");
+
return 0;
+
}
+
+
Dungeon Crawl Stone Soup - My favorite FOSS roguelike (Yes, I know the greatest roguelike of all time is NetHack, I don't care).
-Last updated: Wed Jan 4 12:38:04 2023
+Last updated: Wed Jan 4 13:32:42 2023