Skip to content

Commit

Permalink
Refactor org-hugo-src-block and org-blackfriday-src-block
Browse files Browse the repository at this point in the history
No functional change

Refactoring done for better code clarity and to prep up for the
changes needed in src block export for
#305.
  • Loading branch information
kaushalmodi committed Jan 4, 2022
1 parent fb728ea commit 99eb69e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 89 deletions.
17 changes: 11 additions & 6 deletions ox-blackfriday.el
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,6 @@ information."
(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%s\n%s%s" backticks code-attr ret backticks))
Expand Down Expand Up @@ -1061,7 +1060,11 @@ This function is adapted from `org-html-special-block'."
INFO is a plist used as a communication channel."
(let* ((lang (org-element-property :language src-block))
(lang (or (cdr (assoc lang org-blackfriday-syntax-highlighting-langs)) lang))
(code (org-export-format-code-default src-block info))
(code (or (plist-get info :md-code) ;if set in `org-hugo-src-block'
(org-export-format-code-default src-block info)))
(code-attr (if (plist-get info :md-code-attr) ;if set in `org-hugo-src-block'
(format " { %s }" (plist-get info :md-code-attr))
""))
(parent-element (org-export-get-parent src-block))
(parent-type (car parent-element))
(num-backticks-in-code (when (string-match "^[[:blank:]]*\\(`\\{3,\\}\\)" code)
Expand All @@ -1081,19 +1084,21 @@ INFO is a plist used as a communication channel."
(<= org-blackfriday--code-block-num-backticks num-backticks-in-code))
(setq org-blackfriday--code-block-num-backticks (1+ num-backticks-in-code)))
(setq backticks (make-string org-blackfriday--code-block-num-backticks ?`))
;; (message "[ox-bf src-block DBG]")
;; (message "ox-bf [dbg] code: %s" code)
;; (message "[ox-bf src-block DBG] code: %s" code)
;; (message "[ox-bf src-block DBG] parent type: %S" parent-type)
(setq code (org-blackfriday--issue-239-workaround code parent-type))
(prog1
(format "%s%s\n%s%s" backticks lang code backticks)
(format "%s%s%s\n%s%s" backticks lang code-attr 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))))))
(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))))

;;;; Strike-Through
(defun org-blackfriday-strike-through (_strike-through contents _info)
Expand Down
176 changes: 93 additions & 83 deletions ox-hugo.el
Original file line number Diff line number Diff line change
Expand Up @@ -2691,61 +2691,74 @@ communication channel."
The Markdown style triple-backquoted code blocks are created if:
- If the HUGO_CODE_FENCE property is set to a non-nil value
(default),
- *AND* if none of the Hugo \"highlight\" shortcode features
are needed (see below).
- *AND* if the Hugo \"highlight\" shortcode is not needed (see
below).
The code block is wrapped in Hugo \"highlight\" shortcode (See
https://gohugo.io/content-management/syntax-highlighting) if one
of the above conditions is false.
Hugo v0.60.0 onwards, the `markup.highlight.codeFences' (new name
for the old `pygmentsCodeFences') config variable defaults to
true. See
https://gohugo.io/content-management/syntax-highlighting#highlighting-in-code-fences.
Attributes like highlighting code, \"linenos\", etc. are now
supported with code fences too.
CONTENTS is nil. INFO is a plist used as a communication
channel.
--- Hugo older than v0.60.0 ---
If using a Hugo version older than v0.60.0, note that even with
the default non-nil value of HUGO_CODE_FENCE, the user *needs* to
If using a Hugo version older than v0.60.0, the user *needs* to
set the `pygmentsCodeFences' variable to `true' in their Hugo
site's config. Otherwise syntax highlighting will not work in
the generated fenced code blocks!
Hugo v0.60.0 onwards, the `markup.highlight.codeFences' (new name
for the old `pygmentsCodeFences') config variable defaults to
true.
Hugo \"highlight\" shortcode features:
- Code blocks with line numbers (if the -n or +n switch is used)
Hugo \"highlight\" shortcode is needed if `org-hugo-goldmark' is
nil and,
- Code blocks with line numbers (if the -n or +n switch is used).
- Highlight certains lines in the code block (if the :hl_lines
parameter is used)
parameter is used).
- Set the `linenos' argument to the value passed by :linenos
(defaults to `table')
CONTENTS is nil. INFO is a plist used as a communication
channel."
(defaults to `table')."
(let* ((lang (org-element-property :language src-block))
(parameters-str (org-element-property :parameters src-block))
(parameters (org-babel-parse-header-arguments parameters-str))
(is-fm-extra (cdr (assoc :front_matter_extra parameters)))
(fm-format (plist-get info :hugo-front-matter-format)))
(is-fm-extra (cdr (assoc :front_matter_extra parameters))))
;; (message "ox-hugo src [dbg] lang: %S" lang)
;; (message "ox-hugo src [dbg] is-fm-extra: %S" is-fm-extra)
;; (message "ox-hugo src [dbg] parameters: %S" parameters)
(if (and is-fm-extra
(member lang '("toml" "yaml")))
(progn
;; The fm-extra src block lang and user-set fm-format have to
;; be the same. Else. that src block is completely discarded.
(when (string= lang fm-format)
(let ((fm-extra (org-export-format-code-default src-block info)))
;; (message "ox-hugo src [dbg] fm-extra: %S" fm-extra)
(plist-put info :fm-extra fm-extra)))
;; Do not export the `:front_matter_extra' TOML/YAML source
;; blocks in Markdown body.
nil)
;; (message "ox-hugo src [dbg] is-fm-extra: %S" is-fm-extra)

;; Extra front matter.
(cond
((and is-fm-extra
(member lang '("toml" "yaml")))
(let ((fm-format (plist-get info :hugo-front-matter-format)))
;; The fm-extra src block lang and user-set fm-format have to
;; be the same. Else. that src block is completely discarded.
(when (string= lang fm-format)
(let ((fm-extra (org-export-format-code-default src-block info)))
;; (message "ox-hugo src [dbg] fm-extra: %S" fm-extra)
(plist-put info :fm-extra fm-extra)))
;; Do not export the `:front_matter_extra' TOML/YAML source
;; blocks in Markdown body.
nil))

;; Regular src block.
(t
(let* (;; See `org-element-src-block-parser' for all SRC-BLOCK properties.
(number-lines (org-element-property :number-lines src-block)) ;Non-nil if -n or +n switch is used
(hl-lines (cdr (assoc :hl_lines parameters)))
(linenos-style (cdr (assoc :linenos parameters)))
(hl-lines (cdr (assoc :hl_lines parameters)))
(hl-lines (cond
((stringp hl-lines)
(replace-regexp-in-string "," " " hl-lines)) ;"1,3-4" -> "1 3-4"
((numberp hl-lines)
(number-to-string hl-lines))))
;; Use the `highlight' shortcode if any of the line
;; numbering or highlighting option is set or if
;; HUGO_CODE_FENCE is nil.
(use-highlight-sc (or number-lines
hl-lines
linenos-style
(null (org-hugo--plist-get-true-p info :hugo-code-fence))))
(src-ref (org-blackfriday--get-reference src-block))
(src-anchor (if src-ref
(format "<a id=\"%s\"></a>\n" src-ref)
Expand All @@ -2770,58 +2783,55 @@ channel."
(format "%s %s"
caption-prefix src-block-num))
caption-str))))
content
(src-code (org-hugo--escape-hugo-shortcode
(org-export-format-code-default src-block info)
lang))
code-attr-str
src-code-wrap
ret)
;; (message "ox-hugo src [dbg] number-lines: %S" number-lines)
;; (message "ox-hugo src [dbg] parameters: %S" parameters)
(setq content
(cond
;; If all: number-lines, hl-lines and linenos-style are nil
;; , AND if :hugo-code-fence is non-nil (which is, by default).
((and (null number-lines)
(null hl-lines)
(null linenos-style)
(org-hugo--plist-get-true-p info :hugo-code-fence))
(let ((content1 (org-blackfriday-src-block src-block nil info)))
(setq content1 (org-hugo--escape-hugo-shortcode content1 lang))
content1))
;; If number-lines is non-nil
;; , or if hl-lines is non-nil
;; , or if linenos-style is non-nil
;; , or if :hugo-code-fence is nil
(t
(let ((code (org-export-format-code-default src-block info))
(linenos-str "")
(hllines-str "")
;; Formatter string where the first arg si linenos-str and
;; second is hllines-str.
(highlight-args-str "%s%s"))
(when (or number-lines hl-lines linenos-style)
(setq highlight-args-str " \"%s%s\""))
(when (or number-lines linenos-style)
(let ((linenos-style (or linenos-style "table")))
(setq linenos-str (format "linenos=%s" linenos-style))
(let ((linenostart-str (and ;Extract the start line number of the code block
(string-match "\\`\\s-*\\([0-9]+\\)\\s-\\{2\\}" code)
(match-string-no-properties 1 code))))
(when linenostart-str
(setq linenos-str (format "%s, linenostart=%s" linenos-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 code (replace-regexp-in-string "^\\s-*[0-9]+\\s-\\{2\\}" "" code)))
(when hl-lines
(setq hllines-str (concat "hl_lines=" hl-lines))
(when number-lines
(setq hllines-str (concat ", " hllines-str))))
(setq code (org-hugo--escape-hugo-shortcode code lang))
(format "{{< highlight %s%s >}}\n%s{{< /highlight >}}\n"
lang
(format highlight-args-str linenos-str hllines-str)
code)))))
(setq ret (concat src-anchor content caption-html))
(setq ret (org-blackfriday--div-wrap-maybe src-block ret info))
ret))))

(when (or linenos-style number-lines)
;; Default "linenos" style set to "table" if linenos-style
;; is 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 src block
(string-match "\\`\\s-*\\([0-9]+\\)\\s-\\{2\\}" src-code)
(match-string-no-properties 1 src-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 src-code (replace-regexp-in-string "^\\s-*[0-9]+\\s-\\{2\\}" "" src-code))))

(when hl-lines
(if (org-string-nw-p code-attr-str)
(setq code-attr-str (format "%s, hl_lines=%s" code-attr-str hl-lines))
(setq code-attr-str (format "hl_lines=%s" hl-lines))))

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

(setq src-code-wrap
(if use-highlight-sc
(let ((hl-attr (if (org-string-nw-p code-attr-str)
(format " \"%s\"" code-attr-str)
"")))
(format "{{< highlight %s%s >}}\n%s{{< /highlight >}}\n"
lang hl-attr src-code))
(org-blackfriday-src-block src-block nil info)))

(setq ret (org-blackfriday--div-wrap-maybe
src-block
(concat src-anchor src-code-wrap caption-html)
info))
ret)))))

;;;; Special Block
(defun org-hugo-special-block (special-block contents info)
Expand Down

0 comments on commit 99eb69e

Please sign in to comment.