Skip to content

Commit

Permalink
Example block inherits almost all linenum related features from src b…
Browse files Browse the repository at this point in the history
…lock

Make `org-hugo-example-block` call `org-hugo-src-block` while simply
setting the lang to "text" and setting the :linenos-style parameter if
set by the user in the example block header.

Only :hl_lines won't be supported in example blocks. (But if someone
really! wants to use line highlighting, they can just use the source
blocks.)
  • Loading branch information
kaushalmodi committed Jan 13, 2022
1 parent 2006f06 commit 65f64b1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 65 deletions.
74 changes: 23 additions & 51 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -1759,57 +1759,18 @@ channel."
(defun org-hugo-example-block (example-block _contents info)
"Transcode an EXAMPLE-BLOCK element into Markdown format.
Markdown style triple-backquoted code blocks with \"text\"
\\='language\\=' are 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."
(let* (;; See `org-element-example-block-parser' for all EXAMPLE-BLOCK properties.
(number-lines (org-element-property :number-lines example-block)) ;Non-nil if -n or +n switch is used
(switches-str (org-element-property :switches example-block))
(let* ((switches-str (org-element-property :switches example-block))
;; Below is a hack for allowing ":linenos <value>" parameter
;; in example block header, because the example-block Org
;; element parses only "-switches", not ":parameters".
(linenos-style (and (org-string-nw-p switches-str)
(string-match ":linenos\\s-+\\([^ ]+\\)\\b" switches-str)
(match-string-no-properties 1 switches-str)))
(use-highlight-sc (and (or number-lines linenos-style) ;Use `highlight' shortcode only for Blackfriday if linenos is non-nil
(not (org-hugo--plist-get-true-p info :hugo-goldmark))))
(example-code (org-export-format-code-default example-block info))
code-attr-str
ret)
(when (or number-lines linenos-style)
;; Default "linenos" style set to "table" if only number-lines
;; is non-nil.
(setq linenos-style (or linenos-style "table"))
(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\\}" example-code)
(match-string-no-properties 1 example-code))))
(when linenostart-str
(setq code-attr-str (format "%s, linenostart=%s" code-attr-str linenostart-str))))

(when number-lines
;; 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 example-code (replace-regexp-in-string "^[0-9]+\\s-\\{2\\}" "" example-code))))

(unless use-highlight-sc
(plist-put info :md-code example-code)
(plist-put info :md-code-attr code-attr-str))

(if use-highlight-sc
(setq ret (org-blackfriday--div-wrap-maybe
example-block
(format "{{< highlight text \"%s\" >}}\n%s{{< /highlight >}}\n" code-attr-str example-code)
info))
(setq ret (org-blackfriday-example-block example-block nil info)))
ret))
(match-string-no-properties 1 switches-str))))
(org-element-put-property example-block :language "text")
(org-element-put-property example-block :linenos-style linenos-style)
(org-hugo-src-block example-block nil info)))

;;;; Export Snippet
(defun org-hugo-export-snippet (export-snippet _contents _info)
Expand Down Expand Up @@ -2844,9 +2805,9 @@ communication channel."
"Convert SRC-BLOCK element to Hugo-compatible Markdown.
The Markdown style triple-backquoted code blocks are created if:
- If the HUGO_CODE_FENCE property is set to a non-nil value
- The HUGO_CODE_FENCE property is set to a non-nil value
(default),
- *AND* if the Hugo \"highlight\" shortcode is not needed (see
- *AND* the Hugo \"highlight\" shortcode is not needed (see
below).
Hugo v0.60.0 onwards, the `markup.highlight.codeFences' (new name
Expand All @@ -2859,6 +2820,13 @@ supported with code fences too.
CONTENTS is nil. INFO is a plist used as a communication
channel.
--- Hugo v0.60.0 and newer ---
If using Hugo version v0.60.0 or newer (if `org-hugo-goldmark' is
non-nil), the Hugo \"highlight\" shortcode is needed if,
- Coderefs are used.
--- Hugo older than v0.60.0 ---
If using a Hugo version older than v0.60.0, the user *needs* to
Expand All @@ -2872,7 +2840,8 @@ nil and,
- Highlight certains lines in the code block (if the :hl_lines
parameter is used).
- Set the `linenos' argument to the value passed by :linenos
(defaults to `table')."
(defaults to `table').
- Coderefs are used."
(let* ((lang (org-element-property :language src-block))
(parameters-str (org-element-property :parameters src-block))
(parameters (org-babel-parse-header-arguments parameters-str))
Expand Down Expand Up @@ -2900,7 +2869,10 @@ nil and,
(t
(let* (;; See `org-element-src-block-parser' for all SRC-BLOCK properties.
(line-num-p (org-element-property :number-lines src-block)) ;Non-nil if -n or +n switch is used
(linenos-style (cdr (assoc :linenos parameters)))
(linenos-style (or (cdr (assoc :linenos parameters))
;; If `org-hugo-src-block' is called from
;; `org-hugo-example-block'.
(org-element-property :linenos-style src-block)))
;; Convert `hl-lines' to string. If it's not a number,
;; it's already a string, or nil.
(hl-lines (let* ((hl-lines-param (cdr (assoc :hl_lines parameters))))
Expand Down
21 changes: 15 additions & 6 deletions test/site/content-org/all-posts.org
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,8 @@ Some more text.
:EXPORT_FILE_NAME: coderef
:END:
#+begin_description
Support anchoring the code block lines using Org coderef links.
Support anchoring the source and example block lines using Org coderef
links.
#+end_description

See [[info:org#Literal%20Examples][info:org#Literal Examples]].
Expand All @@ -2017,35 +2018,43 @@ parenthesis.
#+begin_quote
Line numbers are always enabled if coderefs are used. The code blocks
are exported as if the user always added the ~-n~ switches to the
source block header.
source or example block header.
#+end_quote
*** Default line nums with coderef labels
*** Source block
**** Default line nums with coderef labels
#+begin_src emacs-lisp
(save-excursion (ref:sc)
(goto-char (point-min)) (ref:jump)
#+end_src
In line [[(sc)]] we remember the current position. [[(jump)][Line (jump)]] jumps to
point-min.
*** Default line nums without coderef labels
**** Default line nums without coderef labels
#+begin_src emacs-lisp -r
(save-excursion (ref:sc1)
(goto-char (point-min)) (ref:jump1)
#+end_src
In line [[(sc1)]] we remember the current position. [[(jump1)][Line (jump1)]] jumps to
point-min.
*** Custom line nums without coderef labels
**** Custom line nums without coderef labels
#+begin_src emacs-lisp -n 20 -r
(save-excursion (ref:sc2)
(goto-char (point-min)) (ref:jump2)
#+end_src
In line [[(sc2)]] we remember the current position. [[(jump2)][Line (jump2)]] jumps to
point-min.
*** Custom line nums without coderef labels and with highlighting
**** Custom line nums without coderef labels and with highlighting
#+begin_src emacs-lisp -n 20 -r :hl_lines 2
(save-excursion
(goto-char (point-min)) (ref:jump3)
#+end_src
[[(jump3)][Line (jump3)]] jumps to point-min.
*** Example block
#+begin_example -n 20 -r :hl_lines 2
(save-excursion
(goto-char (point-min)) (ref:jump4)
#+end_example
[[(jump4)][Line (jump4)]] jumps to point-min.

** Source blocks with langs that need to be replaced in Markdown :lang:syntax_highlighting:
:PROPERTIES:
:EXPORT_FILE_NAME: source-block-replace-langs-in-exported-md
Expand Down
28 changes: 22 additions & 6 deletions test/site/content/posts/coderef.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
+++
title = "Coderef"
description = "Support anchoring the code block lines using Org coderef links."
description = """
Support anchoring the source and example block lines using Org coderef
links.
"""
tags = ["src-block", "coderef", "annotation"]
draft = false
+++
Expand All @@ -14,10 +17,13 @@ parenthesis.

> Line numbers are always enabled if coderefs are used. The code blocks
> are exported as if the user always added the `-n` switches to the
> source block header.
> source or example block header.

## Default line nums with coderef labels {#default-line-nums-with-coderef-labels}
## Source block {#source-block}


### Default line nums with coderef labels {#default-line-nums-with-coderef-labels}

{{< highlight emacs-lisp "linenos=table, anchorlinenos=true, lineanchors=org-coderef--c1cbed" >}}
(save-excursion (sc)
Expand All @@ -28,7 +34,7 @@ In line [sc](#org-coderef--c1cbed-1) we remember the current position. [Line jum
point-min.


## Default line nums without coderef labels {#default-line-nums-without-coderef-labels}
### Default line nums without coderef labels {#default-line-nums-without-coderef-labels}

{{< highlight emacs-lisp "linenos=table, anchorlinenos=true, lineanchors=org-coderef--ea1413" >}}
(save-excursion
Expand All @@ -39,7 +45,7 @@ In line [1](#org-coderef--ea1413-1) we remember the current position. [Line 2](#
point-min.


## Custom line nums without coderef labels {#custom-line-nums-without-coderef-labels}
### Custom line nums without coderef labels {#custom-line-nums-without-coderef-labels}

{{< highlight emacs-lisp "linenos=table, linenostart=20, anchorlinenos=true, lineanchors=org-coderef--cc4270" >}}
(save-excursion
Expand All @@ -50,11 +56,21 @@ In line [20](#org-coderef--cc4270-20) we remember the current position. [Line 21
point-min.


## Custom line nums without coderef labels and with highlighting {#custom-line-nums-without-coderef-labels-and-with-highlighting}
### Custom line nums without coderef labels and with highlighting {#custom-line-nums-without-coderef-labels-and-with-highlighting}

{{< highlight emacs-lisp "linenos=table, linenostart=20, hl_lines=2, anchorlinenos=true, lineanchors=org-coderef--a1ac71" >}}
(save-excursion
(goto-char (point-min))
{{< /highlight >}}

[Line 21](#org-coderef--a1ac71-21) jumps to point-min.


## Example block {#example-block}

{{< highlight text "linenos=table, linenostart=20, anchorlinenos=true, lineanchors=org-coderef--36657f" >}}
(save-excursion
(goto-char (point-min))
{{< /highlight >}}

[Line 21](#org-coderef--36657f-21) jumps to point-min.
4 changes: 2 additions & 2 deletions test/site/content/posts/highlight-shortcode-src-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ draft = false
Note that to disable the code fence option, the value portion of the
property needs to be left **empty** instead of setting to `nil`!

```text
{{< highlight text >}}
:PROPERTIES:
:EXPORT_HUGO_CODE_FENCE:
:END:
```
{{< /highlight >}}

Here are few variables that you might like to change in the `local.mk`:

Expand Down

0 comments on commit 65f64b1

Please sign in to comment.