From 67ec7988b49a8db4b1fd619458d1d485fef4acbd Mon Sep 17 00:00:00 2001 From: Kaushal Modi Date: Thu, 13 Jul 2017 16:40:41 -0400 Subject: [PATCH] Export Org src blocks to code fenced Markdown by default The option is set by the HUGO_CODE_FENCE property. - If set to t (default), Markdown code fence is used. - Else Hugo `highlight' shortcode is used. Add examples. --- example-site/content-org/all-posts.org | 36 ++++++++++++++++ .../src-blocks-with-highlight-shortcode.org | 41 +++++++++++++++++++ .../content/posts/code-fenced-src-blocks.md | 37 +++++++++++++++++ .../content/posts/shortcode-src-blocks.md | 37 +++++++++++++++++ ox-hugo.el | 24 +++++++---- 5 files changed, 166 insertions(+), 9 deletions(-) create mode 100644 example-site/content-org/src-blocks-with-highlight-shortcode.org create mode 100644 example-site/content/posts/code-fenced-src-blocks.md create mode 100644 example-site/content/posts/shortcode-src-blocks.md diff --git a/example-site/content-org/all-posts.org b/example-site/content-org/all-posts.org index e196cd1..7324017 100644 --- a/example-site/content-org/all-posts.org +++ b/example-site/content-org/all-posts.org @@ -199,6 +199,42 @@ Here is the second post on Emacs. | a | b | e | g | | c | d | f | h | |---+---+---+---| +* Source blocks :src-block: +** Code-fenced source blocks +:PROPERTIES: +:EXPORT_FILE_NAME: code-fenced-src-blocks +:END: +Here are few variables that you might like to change in the =local.mk=: +- =prefix= :: Org installation directory + #+BEGIN_SRC makefile +prefix = /dir/where/you/want/to/install/org # Default: /usr/share +#+END_SRC + The =.el= files will go to =$(prefix)/emacs/site-lisp/org= by + default. If you'd like to change that, you can tweak the + =lispdir= variable. +- =infodir= :: Org Info installation directory. I like to keep the + Info file for development version of Org in a separate + directory. + #+BEGIN_SRC makefile +infodir = $(prefix)/org/info # Default: $(prefix)/info +#+END_SRC +- =ORG_MAKE_DOC= :: Types of Org documentation you'd like to build by + default. + #+BEGIN_SRC makefile +# Define below you only need info documentation, the default includes html and pdf +ORG_MAKE_DOC = info pdf card # html +#+END_SRC +- =ORG_ADD_CONTRIB= :: Packages from the =contrib/= directory that + you'd like to build along with Org. Below are the ones on my + /must-have/ list. + #+BEGIN_SRC makefile +# Define if you want to include some (or all) files from contrib/lisp +# just the filename please (no path prefix, no .el suffix), maybe with globbing +# org-eldoc - Headline breadcrumb trail in minibuffer +# ox-extra - Allow ignoring just the heading, but still export the body of those headings +# org-mime - Convert org buffer to htmlized format for email +ORG_ADD_CONTRIB = org-eldoc ox-extra org-mime +#+END_SRC * TODO Pre-Draft State :PROPERTIES: :EXPORT_FILE_NAME: draft-state-todo diff --git a/example-site/content-org/src-blocks-with-highlight-shortcode.org b/example-site/content-org/src-blocks-with-highlight-shortcode.org new file mode 100644 index 0000000..6eb2535 --- /dev/null +++ b/example-site/content-org/src-blocks-with-highlight-shortcode.org @@ -0,0 +1,41 @@ +#+HUGO_BASE_DIR: ../ +#+HUGO_CODE_FENCE: nil + +#+OPTIONS: loffset:1 +#+SEQ_TODO: TODO DRAFT DONE + +* Source blocks with Hugo =highlight= shortcode +:PROPERTIES: +:EXPORT_FILE_NAME: shortcode-src-blocks +:END: +Here are few variables that you might like to change in the =local.mk=: +- =prefix= :: Org installation directory + #+BEGIN_SRC makefile +prefix = /dir/where/you/want/to/install/org # Default: /usr/share +#+END_SRC + The =.el= files will go to =$(prefix)/emacs/site-lisp/org= by + default. If you'd like to change that, you can tweak the + =lispdir= variable. +- =infodir= :: Org Info installation directory. I like to keep the + Info file for development version of Org in a separate + directory. + #+BEGIN_SRC makefile +infodir = $(prefix)/org/info # Default: $(prefix)/info +#+END_SRC +- =ORG_MAKE_DOC= :: Types of Org documentation you'd like to build by + default. + #+BEGIN_SRC makefile +# Define below you only need info documentation, the default includes html and pdf +ORG_MAKE_DOC = info pdf card # html +#+END_SRC +- =ORG_ADD_CONTRIB= :: Packages from the =contrib/= directory that + you'd like to build along with Org. Below are the ones on my + /must-have/ list. + #+BEGIN_SRC makefile +# Define if you want to include some (or all) files from contrib/lisp +# just the filename please (no path prefix, no .el suffix), maybe with globbing +# org-eldoc - Headline breadcrumb trail in minibuffer +# ox-extra - Allow ignoring just the heading, but still export the body of those headings +# org-mime - Convert org buffer to htmlized format for email +ORG_ADD_CONTRIB = org-eldoc ox-extra org-mime +#+END_SRC diff --git a/example-site/content/posts/code-fenced-src-blocks.md b/example-site/content/posts/code-fenced-src-blocks.md new file mode 100644 index 0000000..30e53e7 --- /dev/null +++ b/example-site/content/posts/code-fenced-src-blocks.md @@ -0,0 +1,37 @@ ++++ +title = "Code-fenced source blocks" +date = 2017-07-13T17:49:22-04:00 +tags = [] +draft = false ++++ + +Here are few variables that you might like to change in the `local.mk`: + +- **`prefix`:** Org installation directory + + ```makefile + prefix = /dir/where/you/want/to/install/org # Default: /usr/share + ``` + + The `.el` files will go to `$(prefix)/emacs/site-lisp/org` by default. If you’d like to change that, you can tweak the `lispdir` variable. +- **`infodir`:** Org Info installation directory. I like to keep the Info file for development version of Org in a separate directory. + + ```makefile + infodir = $(prefix)/org/info # Default: $(prefix)/info + ``` +- **`ORG_MAKE_DOC`:** Types of Org documentation you’d like to build by default. + + ```makefile + # Define below you only need info documentation, the default includes html and pdf + ORG_MAKE_DOC = info pdf card # html + ``` +- **`ORG_ADD_CONTRIB`:** Packages from the `contrib/` directory that you’d like to build along with Org. Below are the ones on my *must-have* list. + + ```makefile + # Define if you want to include some (or all) files from contrib/lisp + # just the filename please (no path prefix, no .el suffix), maybe with globbing + # org-eldoc - Headline breadcrumb trail in minibuffer + # ox-extra - Allow ignoring just the heading, but still export the body of those headings + # org-mime - Convert org buffer to htmlized format for email + ORG_ADD_CONTRIB = org-eldoc ox-extra org-mime + ``` diff --git a/example-site/content/posts/shortcode-src-blocks.md b/example-site/content/posts/shortcode-src-blocks.md new file mode 100644 index 0000000..07c7e40 --- /dev/null +++ b/example-site/content/posts/shortcode-src-blocks.md @@ -0,0 +1,37 @@ ++++ +title = "Source blocks with Hugo `highlight` shortcode" +date = 2017-07-13T17:57:58-04:00 +tags = [] +draft = false ++++ + +Here are few variables that you might like to change in the `local.mk`: + +- **`prefix`:** Org installation directory + + {{< highlight makefile>}} + prefix = /dir/where/you/want/to/install/org # Default: /usr/share + {{< /highlight >}} + + The `.el` files will go to `$(prefix)/emacs/site-lisp/org` by default. If you’d like to change that, you can tweak the `lispdir` variable. +- **`infodir`:** Org Info installation directory. I like to keep the Info file for development version of Org in a separate directory. + + {{< highlight makefile>}} + infodir = $(prefix)/org/info # Default: $(prefix)/info + {{< /highlight >}} +- **`ORG_MAKE_DOC`:** Types of Org documentation you’d like to build by default. + + {{< highlight makefile>}} + # Define below you only need info documentation, the default includes html and pdf + ORG_MAKE_DOC = info pdf card # html + {{< /highlight >}} +- **`ORG_ADD_CONTRIB`:** Packages from the `contrib/` directory that you’d like to build along with Org. Below are the ones on my *must-have* list. + + {{< highlight makefile>}} + # Define if you want to include some (or all) files from contrib/lisp + # just the filename please (no path prefix, no .el suffix), maybe with globbing + # org-eldoc - Headline breadcrumb trail in minibuffer + # ox-extra - Allow ignoring just the heading, but still export the body of those headings + # org-mime - Convert org buffer to htmlized format for email + ORG_ADD_CONTRIB = org-eldoc ox-extra org-mime + {{< /highlight >}} diff --git a/ox-hugo.el b/ox-hugo.el index 3e8617a..7166549 100644 --- a/ox-hugo.el +++ b/ox-hugo.el @@ -108,7 +108,7 @@ directory where all Hugo posts should go by default." (:date "DATE" nil nil) (:tags "TAGS" nil nil 'space) (:hugo-level-offset "HUGO_LEVEL_OFFSET" loffset 0) - (:hugo-tags "HUGO_TAGS" nil nil 'space) ;TODO: Also parse the Org tags as post tags + (:hugo-tags "HUGO_TAGS" nil nil 'space) (:hugo-categories "HUGO_CATEGORIES" nil nil 'space) ;; Optional front matter variables (:hugo-aliases "HUGO_ALIASES" nil nil 'space) @@ -125,7 +125,8 @@ directory where all Hugo posts should go by default." (:with-toc nil "toc" nil) ;No TOC by default (:hugo-section "HUGO_SECTION" nil org-hugo-default-section-directory) (:hugo-base-dir "HUGO_BASE_DIR" nil nil) - (:hugo-static-images "HUGO_STATIC_IMAGES" nil "images"))) + (:hugo-static-images "HUGO_STATIC_IMAGES" nil "images") + (:hugo-code-fence "HUGO_CODE_FENCE" nil "t"))) ;;; Transcode Functions @@ -244,13 +245,18 @@ section as a string." (concat "\n" level-mark " " title " " anchor "\n\n")))) ;;;; Source Blocks -(defun org-hugo-src-block (src-block _contents _info) - "Convert SRC-BLOCK element to the Hugo `highlight' shortcode." - (let* ((lang (org-element-property :language src-block)) - (code (org-element-property :value src-block)) - (shortcode (concat "{{< highlight " lang " >}}\n")) - (close-shortcode "{{< /highlight >}}\n")) - (concat shortcode code close-shortcode))) +(defun org-hugo-src-block (src-block _contents info) + "Convert SRC-BLOCK element to Hugo-compatible element. + + If the HUGO_CODE_FENCE property is set to t (default), the + Markdown style triple-backquoted code blocks are created. + Otherwise, the code block is wrapped in Hugo `highlight' + shortcode." + (if (string= "t" (org-export-data (plist-get info :hugo-code-fence) info)) + (org-blackfriday-src-block src-block nil info) + (let* ((lang (org-element-property :language src-block)) + (code (org-export-format-code-default src-block info))) + (format "{{< highlight %s>}}\n%s{{< /highlight >}}\n" lang code)))) ;;;; Links (defun org-hugo-link (link contents info)