Skip to content

Commit

Permalink
Example blocks use code fence instead of highlight if Goldmark
Browse files Browse the repository at this point in the history
Ref: #305

Also see #485.
  • Loading branch information
kaushalmodi committed Jan 4, 2022
1 parent 87024bd commit 8d2b00c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
11 changes: 9 additions & 2 deletions ox-blackfriday.el
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,16 @@ information."
(let* ((parent-element (org-export-get-parent example-block))
(parent-type (car parent-element))
(backticks (make-string org-blackfriday--code-block-num-backticks ?`))
(example (org-export-format-code-default example-block info))
(example (or (plist-get info :md-code) ;if set in `org-hugo-example-block'
(org-export-format-code-default example-block info)))
(code-attr (if (plist-get info :md-code-attr) ;if set in `org-hugo-example-block'
(format " { %s }" (plist-get info :md-code-attr))
""))
ret)
;; (message "[ox-bf example-block DBG]")
;; (message "[ox-bf example-block DBG] parent type: %S" parent-type)
(setq ret (org-blackfriday--issue-239-workaround example parent-type))
(setq ret (format "%stext\n%s%s" backticks ret backticks))
(setq ret (format "%stext%s\n%s%s" backticks code-attr ret backticks))
(setq ret (org-blackfriday--div-wrap-maybe example-block ret info))
(when (equal 'quote-block parent-type)
;; If the current example block is inside a quote block, future
Expand All @@ -655,6 +659,9 @@ information."
;; for https://github.com/russross/blackfriday/issues/407.
(setq org-blackfriday--code-block-num-backticks
(1+ org-blackfriday--code-block-num-backticks)))
;; Reset the temp info in the `info' plist
(plist-put info :md-code nil)
(plist-put info :md-code-attr nil)
ret))

;;;; Fixed Width
Expand Down
29 changes: 17 additions & 12 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -1746,15 +1746,15 @@ channel."
(defun org-hugo-example-block (example-block _contents info)
"Transcode an EXAMPLE-BLOCK element into Markdown format.
If the example blocks are *not* set to be exported with line
numbers (See (org) Literal examples), Markdown style
triple-backquoted code blocks with \"text\" \\='language\\=' are
created.
Markdown style triple-backquoted code blocks with \"text\"
\\='language\\=' are created.
Otherwise, a \"text\" \\='language\\=' code block wrapped in Hugo
\"highlight\" shortcode (See
https://gohugo.io/content-management/syntax-highlighting) is
created.
If `org-hugo-goldmark' is nil and if the example blocks are *not*
set to be exported with line numbers (See (org) Literal
examples), a \"text\" \\='language\\=' code block wrapped in Hugo
\"highlight\" shortcode is created.
See https://gohugo.io/content-management/syntax-highlighting#highlighting-in-code-fences.
CONTENTS is nil. INFO is a plist holding contextual
information."
Expand All @@ -1764,23 +1764,28 @@ information."
(linenos-style (and (org-string-nw-p switches-str)
(string-match ":linenos\\s-+\\([^ ]+\\)\\b" switches-str)
(match-string-no-properties 1 switches-str)))
linenos-str
code-attr-str
ret)
(if (or number-lines linenos-style)
(let ((linenos-style (or linenos-style "table"))
(text (org-export-format-code-default example-block info)))
(setq linenos-str (format "linenos=%s" linenos-style))
(setq code-attr-str (format "linenos=%s" linenos-style))
(let ((linenostart-str (and ;Extract the start line number of the example block.
(string-match "\\`\\([0-9]+\\)\\s-\\{2\\}" text)
(match-string-no-properties 1 text))))
(when linenostart-str
(setq linenos-str (format "%s, linenostart=%s" linenos-str linenostart-str))))
(setq code-attr-str (format "%s, linenostart=%s" code-attr-str linenostart-str))))

;; Remove Org-inserted numbers from the beginning of each
;; line as the Hugo highlight shortcode will be used instead
;; of literally inserting the line numbers.
(setq text (replace-regexp-in-string "^[0-9]+\\s-\\{2\\}" "" text))
(setq text (format "{{< highlight text \"%s\" >}}\n%s{{< /highlight >}}\n" linenos-str text))
(if (org-hugo--plist-get-true-p info :hugo-goldmark)
(progn
(plist-put info :md-code text)
(plist-put info :md-code-attr code-attr-str)
(setq text (org-blackfriday-example-block example-block nil info)))
(setq text (format "```{{< highlight text \"%s\" >}}\n%s{{< /highlight >}}\n" code-attr-str text)))
(setq ret (org-blackfriday--div-wrap-maybe example-block text info)))
(setq ret (org-blackfriday-example-block example-block nil info)))
ret))
Expand Down
7 changes: 7 additions & 0 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,13 @@ This is an example
line 1
line 2
#+end_example

An example without ~-n~:

#+begin_example
foo
bar
#+end_example
*** Specify new line number start
#+begin_example -n 20
line 20
Expand Down
27 changes: 17 additions & 10 deletions test/site/content/posts/example-block-with-line-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,47 @@ draft = false

## Default new line number start {#default-new-line-number-start}

{{< highlight text "linenos=table, linenostart=1" >}}
```text { linenos=table, linenostart=1 }
line 1
line 2
{{< /highlight >}}
```

An example without `-n`:

```text
foo
bar
```


## Specify new line number start {#specify-new-line-number-start}

{{< highlight text "linenos=table, linenostart=20" >}}
```text { linenos=table, linenostart=20 }
line 20
line 21
{{< /highlight >}}
```


## Default continued line numbers {#default-continued-line-numbers}

{{< highlight text "linenos=table, linenostart=22" >}}
```text { linenos=table, linenostart=22 }
line 22
line 23
{{< /highlight >}}
```


## Specify continued line numbers jump {#specify-continued-line-numbers-jump}

{{< highlight text "linenos=table, linenostart=33" >}}
```text { linenos=table, linenostart=33 }
line 33
line 34
{{< /highlight >}}
```


## Specifying `linenos` parameter {#specifying-linenos-parameter}

{{< highlight text "linenos=false" >}}
```text { linenos=false }
This is line 1
This is line 2
This is line 3
{{< /highlight >}}
```

0 comments on commit 8d2b00c

Please sign in to comment.