diff --git a/ox-blackfriday.el b/ox-blackfriday.el index c2d1569..59196ad 100644 --- a/ox-blackfriday.el +++ b/ox-blackfriday.el @@ -35,6 +35,18 @@ This check is specifically track if that horizontal rule was inserted after the first row of the table.") +(defvar org-blackfriday--code-block-num-backticks-default 3 + "Variable to store the default number of backticks for code block. + +Note that this variable is *only* for internal use.") + +(defvar org-blackfriday--code-block-num-backticks org-blackfriday--code-block-num-backticks-default + "Variable to store the number of backticks for code block. +By default, it stays at 3. This number is incremented for few +corner cases. + +Note that this variable is *only* for internal use.") + ;;; User-Configurable Variables @@ -234,6 +246,12 @@ same column as TABLE-CELL. If no such cookie is found, return ((equal cookie-align "c") 'center) (t 'default))))))) +;;;; Reset org-blackfriday--code-block-num-backticks +(defun org-blackfriday--reset-org-blackfriday--code-block-num-backticks (_backend) + "Reset `org-blackfriday--code-block-num-backticks' to its default value." + (setq org-blackfriday--code-block-num-backticks org-blackfriday--code-block-num-backticks-default)) +(add-hook 'org-export-before-processing-hook #'org-blackfriday--reset-org-blackfriday--code-block-num-backticks) + ;;; Transcode Functions @@ -243,19 +261,49 @@ same column as TABLE-CELL. If no such cookie is found, return "Transcode an EXAMPLE-BLOCK element into Blackfriday Markdown format. CONTENTS is nil. INFO is a plist holding contextual information." - (format "```text\n%s```" - (org-export-format-code-default example-block info))) + (let* ((parent-element (org-export-get-parent example-block)) + (parent-type (car parent-element)) + (backticks (make-string org-blackfriday--code-block-num-backticks ?`))) + ;; (message "[ox-bf example-block DBG]") + ;; (message "[ox-bf example-block DBG] parent type: %S" parent-type) + (prog1 + (format "%stext\n%s%s" + backticks + (org-export-format-code-default example-block info) + backticks) + (when (equal 'quote-block parent-type) + ;; If the current example block is inside a quote block, + ;; future example/code blocks (especially the ones outside + ;; this quote block) will require higher number of backticks. + ;; Workaround for + ;; https://github.com/russross/blackfriday/issues/407. + (setq org-blackfriday--code-block-num-backticks + (1+ org-blackfriday--code-block-num-backticks)))))) ;;;; Fixed Width (defun org-blackfriday-fixed-width (fixed-width _contents info) "Transcode a FIXED-WIDTH element into Blackfriday Markdown format. CONTENTS is nil. INFO is a plist holding contextual information." - (format "```text\n%s```" - (let ((org-src-preserve-indentation t)) - ;; Preserve leading whitespace in the Org Babel Results - ;; blocks. - (org-export-format-code-default fixed-width info)))) + (let* ((parent-element (org-export-get-parent fixed-width)) + (parent-type (car parent-element)) + (backticks (make-string org-blackfriday--code-block-num-backticks ?`))) + (prog1 + (format "%stext\n%s%s" + backticks + (let ((org-src-preserve-indentation t)) + ;; Preserve leading whitespace in the Org Babel Results + ;; blocks. + (org-export-format-code-default fixed-width info)) + backticks) + (when (equal 'quote-block parent-type) + ;; If the current example block is inside a quote block, + ;; future example/code blocks (especially the ones outside + ;; this quote block) will require higher number of backticks. + ;; Workaround for + ;; https://github.com/russross/blackfriday/issues/407. + (setq org-blackfriday--code-block-num-backticks + (1+ org-blackfriday--code-block-num-backticks)))))) ;;;; Footnote Reference (defun org-blackfriday-footnote-reference (footnote-reference _contents info) @@ -363,6 +411,7 @@ communication channel." (next-type (org-element-type next)) (next-is-quote (eq 'quote-block next-type)) (contents (org-md-quote-block quote-block contents info))) + ;; (message "[ox-bf quote-block DBG]") (concat contents ;; Two consecutive blockquotes in Markdown can be ;; separated by a comment. @@ -377,9 +426,11 @@ INFO is a plist used as a communication channel." (let* ((lang (org-element-property :language src-block)) (code (org-export-format-code-default src-block info)) (parent-element (org-export-get-parent src-block)) - (parent-type (car parent-element))) + (parent-type (car parent-element)) + (backticks (make-string org-blackfriday--code-block-num-backticks ?`))) + ;; (message "[ox-bf src-block DBG]") ;; (message "ox-bf [dbg] code: %s" code) - ;; (message "dbg parent type: %S" parent-type) + ;; (message "[ox-bf src-block DBG] parent type: %S" parent-type) ;; Hack to avert a bug in Blackfriday ;; Details: https://github.com/kaushalmodi/ox-hugo/issues/57 ;; Prefix the ASTERISK (0x2a), PLUS SIGN (0x2b) and HYPHEN-MINUS @@ -392,7 +443,15 @@ INFO is a plist used as a communication channel." ;; There's a ZERO WIDTH SPACE char (0x200b) here ^^, ;; (after «"», but before «\\&"» above) ;; It's not visible (because zero width), but it's there. - (format "```%s\n%s```" lang code))) + (prog1 + (format "%s%s\n%s%s" backticks lang code backticks) + (when (equal 'quote-block parent-type) + ;; If the current code block is inside a quote block, future + ;; example/code blocks (especially the ones outside this quote + ;; block) will require higher number of backticks. Workaround + ;; for https://github.com/russross/blackfriday/issues/407. + (setq org-blackfriday--code-block-num-backticks + (1+ org-blackfriday--code-block-num-backticks)))))) ;;;; Strike-Through (defun org-blackfriday-strike-through (_strike-through contents _info) diff --git a/ox-hugo.el b/ox-hugo.el index dd5754d..75066f0 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -810,7 +810,7 @@ information." ;; of literally inserting the line numbers. (setq text (replace-regexp-in-string "^[0-9]+\\s-\\{2\\}" "" text)) (format "{{< highlight text %s>}}\n%s{{< /highlight >}}\n" linenos-str text)) - (format "```text\n%s```" text)))) + (org-blackfriday-example-block example-block nil info)))) ;;;; Headline (defun org-hugo-headline (headline contents info) diff --git a/test/site/content-org/all-posts.org b/test/site/content-org/all-posts.org index 240aae2..3c9579e 100644 --- a/test/site/content-org/all-posts.org +++ b/test/site/content-org/all-posts.org @@ -1665,6 +1665,102 @@ Quote 3. This is a multi-paragraph quote. This is the second paragraph. #+END_QUOTE Some other text. +** Example block inside quote block +:PROPERTIES: +:EXPORT_FILE_NAME: example-block-inside-quote-block +:END: +Some text. +#+BEGIN_QUOTE +Some quoted text. + + #+BEGIN_EXAMPLE + (some-example) + #+END_EXAMPLE +#+END_QUOTE +Some other text. +** Multiple example blocks inside quote block +:PROPERTIES: +:EXPORT_FILE_NAME: multiple-example-blocks-inside-quote-block +:END: +Some text. +#+BEGIN_QUOTE +Some quoted text. + + #+BEGIN_EXAMPLE + (some-example) + #+END_EXAMPLE + #+BEGIN_EXAMPLE + (some-other-example) + #+END_EXAMPLE +#+END_QUOTE +Some other text. +** Source block inside quote block, followed by another source block outside +:PROPERTIES: +:EXPORT_FILE_NAME: source-block-inside-quote-block-and-another-source-block +:END: +[[https://github.com/russross/blackfriday/issues/407][Blackfriday Issue # 407]] + +Some text. +#+BEGIN_QUOTE +Some quoted text. + #+BEGIN_SRC emacs-lisp + (message "hello") + #+END_SRC +#+END_QUOTE +#+BEGIN_SRC emacs-lisp +(message "hello again") +#+END_SRC +Some other text. +** Example blocks inside quote block, followed by another example block outside +:PROPERTIES: +:EXPORT_FILE_NAME: example-blocks-inside-quote-block-and-another-example-block +:END: +[[https://github.com/russross/blackfriday/issues/407][Blackfriday Issue # 407]] + +Some text. +#+BEGIN_QUOTE +Some quoted text. + #+BEGIN_EXAMPLE + (some-example) + #+END_EXAMPLE + #+BEGIN_EXAMPLE + (some-other-example) + #+END_EXAMPLE +#+END_QUOTE + +#+BEGIN_EXAMPLE +(yet-another-example) +#+END_EXAMPLE +Some other text. +** Source block, followed by a quote block containing another source block +:PROPERTIES: +:EXPORT_FILE_NAME: source-block-followed-by-a-quote-block-containing-another-source-block +:END: +Some text. +#+BEGIN_SRC emacs-lisp +(message "hello") +#+END_SRC +#+BEGIN_QUOTE +Some quoted text. + #+BEGIN_SRC emacs-lisp + (message "hello again") + #+END_SRC +#+END_QUOTE +Some other text. +** Example block with escaped Org syntax inside quote block +:PROPERTIES: +:EXPORT_FILE_NAME: example-block-with-escaped-org-inside-quote-block +:END: +Some text. +#+BEGIN_QUOTE +Some quoted text. + + #+BEGIN_EXAMPLE + ,#+NAME: some_example + (some-example) + #+END_EXAMPLE +#+END_QUOTE +Some other text. * Verse :verse: ** One verse :PROPERTIES: diff --git a/test/site/content/posts/example-block-inside-quote-block.md b/test/site/content/posts/example-block-inside-quote-block.md new file mode 100644 index 0000000..20a99c2 --- /dev/null +++ b/test/site/content/posts/example-block-inside-quote-block.md @@ -0,0 +1,15 @@ ++++ +title = "Example block inside quote block" +tags = ["quotes"] +draft = false ++++ + +Some text. + +> Some quoted text. +> +> ```text +> (some-example) +> ``` + +Some other text. diff --git a/test/site/content/posts/example-block-with-escaped-org-inside-quote-block.md b/test/site/content/posts/example-block-with-escaped-org-inside-quote-block.md new file mode 100644 index 0000000..0c1e837 --- /dev/null +++ b/test/site/content/posts/example-block-with-escaped-org-inside-quote-block.md @@ -0,0 +1,16 @@ ++++ +title = "Example block with escaped Org syntax inside quote block" +tags = ["quotes"] +draft = false ++++ + +Some text. + +> Some quoted text. +> +> ```text +> #+NAME: some_example +> (some-example) +> ``` + +Some other text. diff --git a/test/site/content/posts/example-blocks-inside-quote-block-and-another-example-block.md b/test/site/content/posts/example-blocks-inside-quote-block-and-another-example-block.md new file mode 100644 index 0000000..258219b --- /dev/null +++ b/test/site/content/posts/example-blocks-inside-quote-block-and-another-example-block.md @@ -0,0 +1,25 @@ ++++ +title = "Example blocks inside quote block, followed by another example block outside" +tags = ["quotes"] +draft = false ++++ + +[Blackfriday Issue # 407](https://github.com/russross/blackfriday/issues/407) + +Some text. + +> Some quoted text. +> +> ```text +> (some-example) +> ``` +> +> ````text +> (some-other-example) +> ```` + +`````text +(yet-another-example) +````` + +Some other text. diff --git a/test/site/content/posts/multiple-example-blocks-inside-quote-block.md b/test/site/content/posts/multiple-example-blocks-inside-quote-block.md new file mode 100644 index 0000000..4053e06 --- /dev/null +++ b/test/site/content/posts/multiple-example-blocks-inside-quote-block.md @@ -0,0 +1,19 @@ ++++ +title = "Multiple example blocks inside quote block" +tags = ["quotes"] +draft = false ++++ + +Some text. + +> Some quoted text. +> +> ```text +> (some-example) +> ``` +> +> ````text +> (some-other-example) +> ```` + +Some other text. diff --git a/test/site/content/posts/source-block-followed-by-a-quote-block-containing-another-source-block.md b/test/site/content/posts/source-block-followed-by-a-quote-block-containing-another-source-block.md new file mode 100644 index 0000000..61e52fd --- /dev/null +++ b/test/site/content/posts/source-block-followed-by-a-quote-block-containing-another-source-block.md @@ -0,0 +1,19 @@ ++++ +title = "Source block, followed by a quote block containing another source block" +tags = ["quotes"] +draft = false ++++ + +Some text. + +```emacs-lisp +(message "hello") +``` + +> Some quoted text. +> +> ```emacs-lisp +> (message "hello again") +> ``` + +Some other text. diff --git a/test/site/content/posts/source-block-inside-quote-block-and-another-source-block.md b/test/site/content/posts/source-block-inside-quote-block-and-another-source-block.md new file mode 100644 index 0000000..694404f --- /dev/null +++ b/test/site/content/posts/source-block-inside-quote-block-and-another-source-block.md @@ -0,0 +1,21 @@ ++++ +title = "Source block inside quote block, followed by another source block outside" +tags = ["quotes"] +draft = false ++++ + +[Blackfriday Issue # 407](https://github.com/russross/blackfriday/issues/407) + +Some text. + +> Some quoted text. +> +> ```emacs-lisp +> (message "hello") +> ``` + +````emacs-lisp +(message "hello again") +```` + +Some other text.