Added support for markdown tables

This commit is contained in:
j4nk 2024-03-30 20:03:25 -04:00
parent 0e2d7e7aeb
commit 5770c60bcf
5 changed files with 49 additions and 7 deletions

View File

@ -17,7 +17,7 @@ A language and parser written in Emacs Lisp for my custom extension of Markdown,
- [x] Image - [x] Image
### Extended Syntax ### Extended Syntax
- [ ] Table - [x] Table
- [x] Fenced Code Block - [x] Fenced Code Block
- [ ] Footnote (Will not implement) - [ ] Footnote (Will not implement)
- [ ] Heading ID (Will not implement) - [ ] Heading ID (Will not implement)

View File

@ -1,4 +1,4 @@
<rss version="2.0"><channel><title>RSS Test</title><link>http://example.com</link><description>An example blog for testing RSS capability of md4tj.</description><language>en-US</language><pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate><managingEditor>editor@example.com</managingEditor><webMaster>webmaster@example.com</webMaster><docs>https://www.rssboard.org/rss-specification</docs><generator>md4tj-rss.el</generator><lastBuildDate>Tue, 05 Sep 2023 00:44:58 GMT</lastBuildDate><item><title>Blog Post 2</title><link>https://example.com/blog/blogpost2.html</link><guid>https://example.com/blog/blogpost2.html</guid><description> <rss version="2.0"><channel><title>RSS Test</title><link>http://example.com</link><description>An example blog for testing RSS capability of md4tj.</description><language>en-US</language><pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate><managingEditor>editor@example.com</managingEditor><webMaster>webmaster@example.com</webMaster><docs>https://www.rssboard.org/rss-specification</docs><generator>md4tj-rss.el</generator><lastBuildDate>Sat, 30 Mar 2024 19:58:58 GMT</lastBuildDate><item><title>Blog Post 2</title><link>https://example.com/blog/blogpost2.html</link><guid>https://example.com/blog/blogpost2.html</guid><description>
&lt;br&gt; &lt;br&gt;

View File

@ -8,7 +8,8 @@
(require 'subr-x) (require 'subr-x)
;; Change this line to wherever dash.el is on the building system ;; Change this line to wherever dash.el is on the building system
;; Can't use (require) because load-path is set in .emacs ;; Can't use (require) because load-path is set in .emacs
(load-file "~/.emacs.d/elpa/dash-20230415.2324/dash.el") ;;(load-file "~/.emacs.d/elpa/dash-20230415.2324/dash.el")
(load-file "~/.emacs.d/elpa/dash-20240103.1301/dash.el")
;; Basic utilities for subsequent stuff ;; Basic utilities for subsequent stuff
(defun md4tj-util-getline () (defun md4tj-util-getline ()
@ -94,6 +95,13 @@
"`\\(.*\\)`" "`\\(.*\\)`"
"<code>\\1</code>" line))))))))) "<code>\\1</code>" line)))))))))
(defun md4tj-handle-table-row (line header)
"Return the string for a table row that is a HEADER from LINE."
(let ((opentag (if header "<th>" "<td>"))
(closetag (if header "</th>" "</td>")))
(if (string-match "^|---.*" line) "" ;; do nothing if the line under the header for the table
(concat "<tr>\n" (mapconcat (lambda (x) (concat opentag (md4tj-process-line x) closetag "\n")) (split-string line "|" t " *")) "</tr>"))))
(defun md4tj-convert-line-to-html (line state inbuf outfile) (defun md4tj-convert-line-to-html (line state inbuf outfile)
"Process LINE with STATE and return html, INBUF provided with OUTFILE." "Process LINE with STATE and return html, INBUF provided with OUTFILE."
(let ((cleanline (md4tj-util-clean-multiline line))) (let ((cleanline (md4tj-util-clean-multiline line)))
@ -133,6 +141,8 @@
((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
((eq (nth 1 state) 'begintable) (md4tj-handle-table-row cleanline t))
((eq (nth 1 state) 'table) (md4tj-handle-table-row cleanline nil))
(t (md4tj-process-paragraph (md4tj-process-line cleanline)))) (t (md4tj-process-paragraph (md4tj-process-line cleanline))))
;; End of multiline block ;; End of multiline block
@ -152,6 +162,8 @@
((eq state 'endul) "</ul>\n") ((eq state 'endul) "</ul>\n")
((eq state 'endol) "</ol>\n") ((eq state 'endol) "</ol>\n")
((eq state 'endcode) "</code>\n</pre>\n") ((eq state 'endcode) "</code>\n</pre>\n")
((eq state 'begintable) "<table>") ;; might need to add to this?
((eq state 'endtable) "</table>")
(t ""))) (t "")))
(defun md4tj-next-state (currline prevstate) (defun md4tj-next-state (currline prevstate)
@ -163,6 +175,7 @@
((and (not (string-match "^- " currline)) (or (eq prevstate 'beginul) (eq prevstate 'ul))) '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 (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) ((and (string-match "```$" currline) (or (eq prevstate 'code) (eq prevstate 'begincode))) 'endcode)
((and (not (string-match "^|.*|$" currline)) (or (eq prevstate 'begintable) (eq prevstate 'headtable) (eq prevstate 'table))) 'endtable)
(t 'nothing)) (t 'nothing))
;; Begin state (or next line's prevstate) ;; Begin state (or next line's prevstate)
@ -172,6 +185,8 @@
((and (string-match "^[0-9]+\\. " currline) (or (eq prevstate 'beginol) (eq prevstate 'ol)) 'ol)) ((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 (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 (not (string-match "```$" currline)) (or (eq prevstate 'begincode) (eq prevstate 'code)) 'code))
((and (string-match "^|.*|$" currline) (not (or (eq prevstate 'table) (eq prevstate 'begintable)))) 'begintable)
((and (string-match "^|.*|$" currline) (or (eq prevstate 'begintable) (eq prevstate 'table))) 'table)
(t 'normal)))) (t 'normal))))
(defun md4tj-begin () (defun md4tj-begin ()
@ -189,6 +204,7 @@
(cond ((or (eq state 'beginul) (eq state 'ul)) "</ul>") (cond ((or (eq state 'beginul) (eq state 'ul)) "</ul>")
((or (eq state 'beginol) (eq state 'ol)) "</ol>") ((or (eq state 'beginol) (eq state 'ol)) "</ol>")
((or (eq state 'begincode) (eq state 'code)) "</code>") ((or (eq state 'begincode) (eq state 'code)) "</code>")
((or (eq state 'begintable) (eq state 'table)) "</table>")
(t "")) (t ""))
"</body>\n" "</body>\n"
"</html>")) "</html>"))

View File

@ -18,7 +18,7 @@
<h3>Hello world!</h3> <h3>Hello world!</h3>
<h4>Hello <strong><em>world</em></strong>!</h4> <h4>Hello <strong><em>world</strong></em>!</h4>
<h1>This was included from a separate file!</h1> <h1>This was included from a separate file!</h1>
@ -75,7 +75,7 @@ int main() {
<p>This is a div that has monospace text.</p> <p>This is a div that has monospace text.</p>
</div> </div>
<p id="lastupdated">Last updated: Tue 05 Sep 2023 04:44 UTC</p> <p id="lastupdated">Last updated: Sat 30 Mar 2024 23:58 UTC</p>
<br> <br>
@ -92,5 +92,25 @@ int main() {
<p><a href="https://en.wikipedia.org/wiki/Talk:Joy,_Departed">Joy, Departed</a></p> <p><a href="https://en.wikipedia.org/wiki/Talk:Joy,_Departed">Joy, Departed</a></p>
<p><a href="https://support.logi.com/hc/en-us/articles/360023467053-G13-Technical-Specifications">Logitech G13</a></p> <p><a href="https://support.logi.com/hc/en-us/articles/360023467053-G13-Technical-Specifications">Logitech G13</a></p>
<br>
<table><tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td><em>Item 1</em></td>
<td><strong>Item 2</strong></td>
<td><s>Item 3</s></td>
</tr>
</table>
<br>
<p>Content after the table</p>
</body> </body>
</html> </html>

View File

@ -39,3 +39,9 @@ This is a div that has monospace text.
[The Operative: No One Lives Forever](https://en.wikipedia.org/wiki/The_Operative:_No_One_Lives_Forever) [The Operative: No One Lives Forever](https://en.wikipedia.org/wiki/The_Operative:_No_One_Lives_Forever)
[Joy, Departed](https://en.wikipedia.org/wiki/Talk:Joy,_Departed) [Joy, Departed](https://en.wikipedia.org/wiki/Talk:Joy,_Departed)
[Logitech G13](https://support.logi.com/hc/en-us/articles/360023467053-G13-Technical-Specifications) [Logitech G13](https://support.logi.com/hc/en-us/articles/360023467053-G13-Technical-Specifications)
| Header 1 | Header 2 | Header 3|
|----------|----------|---------|
| *Item 1* | **Item 2** | ~~Item 3~~ |
Content after the table