diff --git a/md4tj-blog.el b/md4tj-blog.el index ba3c8be..3818f0d 100644 --- a/md4tj-blog.el +++ b/md4tj-blog.el @@ -1,5 +1,6 @@ ;;; md4tj --- Summary + ;;; Commentary: ;;; Code: @@ -7,11 +8,13 @@ (require 'cl-lib) ;(load "./md4tj-util.el") -(defun md4tj-blog-enabled () - "Determine if the current buffer corresponds to a blog." - (save-excursion - (goto-char (point-min)) - (not (not (search-forward-regexp "^@@BLOGENABLE" (point-max) t))))) +;; NOT needed anymore +;; Presence of blog indicated by @@BLOGINSERT line +;; (defun md4tj-blog-enabled () +;; "Determine if the current buffer corresponds to a blog." +;; (save-excursion +;; (goto-char (point-min)) +;; (not (not (search-forward-regexp "^@@BLOGENABLE" (point-max) t))))) (defun md4tj-blog-base-url () "Find base URL of blog." @@ -26,17 +29,20 @@ (save-excursion (goto-char (point-min)) (if (search-forward-regexp "^@@BLOGBASEDIR" (point-max) t) - (replace-regexp-in-string "^@@BLOGBASEDIR[ \\\t]+" "" (md4tj-util-getline)) - ""))) + (let ((tmp (replace-regexp-in-string "^@@BLOGBASEDIR[ \\\t]+" "" (md4tj-util-getline)))) + (if (string-match "/$" tmp) tmp (concat tmp "/")))))) +;; (defun md4tj-blog-names () +;; "Find all blogs on blog page." +;; (cl-map #'listp (lambda (l) (nth 1 l)) (cl-map #'listp 'split-string (cl-remove-if-not (lambda (s) (string-match "^@@BLOGPOST" s)) (split-string (buffer-string) "\n"))))) (defun md4tj-blog-names () - "Find all blogs on blog page." - (cl-map #'listp (lambda (l) (nth 1 l)) (cl-map #'listp 'split-string (cl-remove-if-not (lambda (s) (string-match "^@@BLOGPOST" s)) (split-string (buffer-string) "\n"))))) + "Get all blogs names from buffer." + (cl-map 'listp (lambda (s) (string-replace (md4tj-blog-base-dir) "" s)) (cl-map 'listp (lambda (s) (replace-regexp-in-string "\\.md4tj$" "" s)) (md4tj-blog-files)))) (defun md4tj-blog-links () "Get all links on the blog page." (let ((base-url (md4tj-blog-base-url))) - (cl-map #'listp (lambda (s) (concat base-url s)) (md4tj-blog-names)))) + (cl-map #'listp (lambda (s) (concat base-url s ".html")) (md4tj-blog-names)))) (defun md4tj-blog-file (name) "Get full filename from NAME." @@ -44,7 +50,8 @@ (defun md4tj-blog-files () "Get all blog .md4tj files." - (cl-map #'listp 'md4tj-blog-file (md4tj-blog-names))) + (cl-map 'listp (lambda (s) (concat (md4tj-blog-base-dir) s)) (cl-remove-if-not (lambda (s) (string-match ".*\\.md4tj$" s)) (directory-files (md4tj-blog-base-dir))))) + ;(cl-map #'listp 'md4tj-blog-file (md4tj-blog-names))) (defun md4tj-blog-title (f) "Get blog title from file F." @@ -77,24 +84,26 @@ ;; Title ;; Time ;; Link -(defun md4tj-all-blogs-list () +(defun md4tj-blog-all-blogs-list () "Get all blogs in current buffer." (-sort (lambda (l1 l2) (< (string-to-number (nth 1 l1)) (string-to-number (nth 1 l2)))) (md4tj-util-zip (list (md4tj-blog-titles) (md4tj-blog-times) (md4tj-blog-links))))) -(defun md4tj-all-blogs-list-elt-to-html (elt) +(defun md4tj-blog-all-blogs-list-elt-to-html (elt) "Convert abl list elt ELT to html." (concat "
\\1
" line)))))))))
-(defun md4tj-convert-line-to-html (line state)
- "Process LINE with STATE and return html."
+(defun md4tj-convert-line-to-html (line state inbuf)
+ "Process LINE with STATE and return html, INBUF provided."
(let ((cleanline (md4tj-util-clean-multiline line)))
;; If this is a signal to include another file
(cond ((string-match "^@@INCLUDE" line) (md4tj-parse-to-string (nth 1 (split-string line))))
@@ -76,6 +78,7 @@
(md4tj-end-tag "p")))
((string-match "^@@DIV" line) (md4tj-begin-tag "div" (list (list "class" (nth 1 (split-string line))))))
((string-match "^@@ENDDIV" line) (md4tj-end-tag "div"))
+ ((string-match "^@@BLOGINSERT" line) (with-current-buffer inbuf (md4tj-blog-html)))
((and (string-match "^$$.*$$" line) (eq (nth 1 state) 'normal)) ;; LaTeX formula
(shell-command (concat "./pnglatex"
" -d 300"
@@ -212,13 +215,17 @@
(setq line (with-current-buffer inbuf (md4tj-util-getline)))
(setq fullstate (md4tj-next-state line (nth 1 fullstate)))
;; Insert next line(s) into output file
- (insert (concat (md4tj-convert-line-to-html line fullstate) "\n"))
+ (let ((linehtml (md4tj-convert-line-to-html line fullstate inbuf)))
+ (insert (concat linehtml (if (string-empty-p linehtml) "" "\n"))))
;; Advance input file by a line
(with-current-buffer inbuf (forward-line)))
(insert (md4tj-finalize (nth 1 fullstate)))
;; Write outbuf to outfile
(write-region nil nil outfile nil)))
+;; NOTE: Cannot md4tj-parse-to-string a file with a blog
+;; This is fine, as included files are mostly meant to be stuff like
+;; navbars, etc.
(defun md4tj-parse-to-string (mdfile)
"Parse MDFILE, return conversion to HTML as string."
(with-temp-buffer
@@ -230,7 +237,7 @@
(while (< (point) (point-max))
(setq line (md4tj-util-getline))
(setq fullstate (md4tj-next-state line (nth 1 fullstate)))
- (setq acc (concat acc (md4tj-convert-line-to-html line fullstate) "\n"))
+ (setq acc (concat acc (md4tj-convert-line-to-html line fullstate nil) "\n"))
(forward-line))
acc)))
diff --git a/test_blog.md4tj b/test_blog.md4tj
index 3709573..f21e80c 100644
--- a/test_blog.md4tj
+++ b/test_blog.md4tj
@@ -8,10 +8,12 @@
@@RSSCHANNELdocs https://www.rssboard.org/rss-specification
@@RSSCHANNELmanagingEditor editor@example.com
@@RSSCHANNELwebMaster webmaster@example.com
+@@TITLE Test blog
-@@BLOGENABLE
-@@BLOGBASEURL ./blog/
+# Blogs
+
+@@BLOGBASEURL https://example.com/blog/
@@BLOGBASEDIR ./blog/
+@@BLOGINSERT
-@@BLOGPOST blogpost1
-@@BLOGPOST blogpost2
\ No newline at end of file
+### Non blog text!!
\ No newline at end of file