Skip to content

Commit

Permalink
Merge pull request #496 from kaushalmodi/remove-extra-divs
Browse files Browse the repository at this point in the history
Remove extra divs
  • Loading branch information
kaushalmodi committed Jan 4, 2022
2 parents e2bc6c4 + ec88b9a commit 100010c
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 81 deletions.
81 changes: 50 additions & 31 deletions ox-blackfriday.el
Expand Up @@ -85,9 +85,9 @@ customized. But there are some src block \"languages\" like
tag needs to be `python'."
:group 'org-export-blackfriday
:type '(repeat
(cons
(string "Src Block language")
(string "Syntax highlighting language"))))
(cons
(string "Src Block language")
(string "Syntax highlighting language"))))



Expand Down Expand Up @@ -165,6 +165,23 @@ INFO is a plist used as a communication channel."
(org-export-get-reference heading info))))
(concat indent "- [" title "]" "(#" anchor ")")))

;;;; Extra div hack
(defun org-blackfriday--extra-div-hack (info &optional tag)
"Return string for the \"extra div hack\".
The empty HTML element tags like \"<div></div>\" is a hack to get
around a Blackfriday limitation.
See https://github.com/kaushalmodi/ox-hugo/issues/93.
INFO is a plist used as a communication channel.
If TAG is not specified, it defaults to \"div\"."
(let ((tag (or tag "div")))
(if (org-blackfriday--plist-get-true-p info :hugo-goldmark)
""
(format "\n <%s></%s>" tag tag))))

;;;; Footnote section
(defun org-blackfriday-footnote-section (info &optional is-cjk)
"Format the footnote section.
Expand Down Expand Up @@ -417,9 +434,11 @@ This function is adapted from `org-html--make-attribute-string'."
(setcar ret (format "%s: %s; " key value))))))))

;;;; Wrap with HTML attributes
(defun org-blackfriday--div-wrap-maybe (elem contents)
(defun org-blackfriday--div-wrap-maybe (elem contents info)
"Wrap the CONTENTS with HTML div tags.
INFO is a plist used as a communication channel.
The div wrapping is done only if HTML attributes are set for the
ELEM Org element using #+attr_html.
Expand Down Expand Up @@ -460,9 +479,9 @@ style tag."

(setq ret (concat style-str
(if contents
(format "<div %s>\n <div></div>\n\n%s\n</div>" ;See footnote 1
attr-str contents)
"")))))
(format "<div %s>%s\n\n%s\n</div>"
attr-str (org-blackfriday--extra-div-hack info) contents))
""))))
ret))

;;;; Sanitize URL
Expand Down Expand Up @@ -600,12 +619,14 @@ Credit: https://emacs.stackexchange.com/a/53433/115."
;;; Transcode Functions

;;;; Center Block
(defun org-blackfriday-center-block (_center-block contents _info)
"Center-align the text in CONTENTS using CSS."
(defun org-blackfriday-center-block (_center-block contents info)
"Center-align the text in CONTENTS using CSS.
INFO is a plist used as a communication channel."
(let* ((class "org-center")
(style (format ".%s { margin-left: auto; margin-right: auto; text-align: center; }" class)))
(format "<style>%s</style>\n\n<div class=\"%s\">\n <div></div>\n\n%s\n</div>" ;See footnote 1
style class contents)))
(format "<style>%s</style>\n\n<div class=\"%s\">%s\n\n%s\n</div>"
style class (org-blackfriday--extra-div-hack info) contents)))

;;;; Example Block
(defun org-blackfriday-example-block (example-block _contents info)
Expand All @@ -621,7 +642,7 @@ information."
;; (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 (org-blackfriday--div-wrap-maybe example-block ret))
(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
;; example/code blocks (especially the ones outside this quote
Expand All @@ -648,7 +669,8 @@ information."
;; Preserve leading whitespace in the Org Babel Results
;; blocks.
(org-export-format-code-default fixed-width info))
backticks))
backticks)
info)
(when (equal 'quote-block parent-type)
;; If the current example block is inside a quote block,
;; future example/code blocks (especially the ones outside
Expand Down Expand Up @@ -818,12 +840,12 @@ communication channel."
;; If this is an ordered list and if any item in this list is
;; using a custom counter, export this list in HTML.
(setq ret (concat
(org-blackfriday--div-wrap-maybe plain-list nil)
(org-blackfriday--div-wrap-maybe plain-list nil info)
(org-html-plain-list plain-list contents info)))
(let* ((next (org-export-get-next-element plain-list info))
(next-type (org-element-type next)))
;; (message "content: `%s', next type: %s" contents next-type)
(setq ret (org-blackfriday--div-wrap-maybe plain-list contents))
(setq ret (org-blackfriday--div-wrap-maybe plain-list contents info))
(when (member next-type '(plain-list
src-block example-block)) ;https://github.com/russross/blackfriday/issues/556
(setq ret (concat ret "\n<!--listend-->")))))
Expand Down Expand Up @@ -885,7 +907,7 @@ communication channel."
(contents (org-md-quote-block quote-block contents info))
ret)
;; (message "[ox-bf quote-block DBG]")
(setq ret (org-blackfriday--div-wrap-maybe quote-block contents))
(setq ret (org-blackfriday--div-wrap-maybe quote-block contents info))
(setq ret (concat ret
;; Two consecutive blockquotes in Markdown can be
;; separated by a comment.
Expand All @@ -894,10 +916,12 @@ communication channel."
ret))

;;;; Special Block
(defun org-blackfriday-special-block (special-block contents _info)
(defun org-blackfriday-special-block (special-block contents info)
"Transcode a SPECIAL-BLOCK element from Org to HTML.
CONTENTS holds the contents of the block.
INFO is a plist used as a communication channel.
This function is adapted from `org-html-special-block'."
(let* ((block-type (org-element-property :type special-block))
(html5-inline-fancy (member block-type org-blackfriday-html5-inline-elements))
Expand Down Expand Up @@ -979,11 +1003,13 @@ This function is adapted from `org-html-special-block'."
(format "<%s%s>%s</%s>"
block-type attr-str contents block-type))))
(html5-block-fancy
(format "<%s%s>\n <%s></%s>\n\n%s\n\n</%s>" ;See footnote 1
block-type attr-str block-type block-type contents block-type))
(format "<%s%s>%s\n\n%s\n\n</%s>"
block-type attr-str
(org-blackfriday--extra-div-hack info block-type)
contents block-type))
(t
(format "<div%s>\n <div></div>\n\n%s\n\n</div>" ;See footnote 1
attr-str contents))))))
(format "<div%s>%s\n\n%s\n\n</div>"
attr-str (org-blackfriday--extra-div-hack info) contents))))))

;;;; Src Block
(defun org-blackfriday-src-block (src-block _contents info)
Expand Down Expand Up @@ -1200,8 +1226,9 @@ contextual information."
(when (or (org-string-nw-p table-class-user)
(org-string-nw-p css-props-str))
(setq table-pre (concat table-pre
(format "<div class=\"ox-hugo-table %s\">\n" table-class)
"<div></div>\n"))) ;See footnote 1
(format "<div class=\"ox-hugo-table %s\">%s\n"
table-class
(org-blackfriday--extra-div-hack info)))))
(when (org-string-nw-p table-pre)
(setq table-post (concat "\n"
"</div>\n")))
Expand Down Expand Up @@ -1341,12 +1368,4 @@ Return output file name."
(provide 'ox-blackfriday)



;;; Footnotes

;;;; Footnote 1
;; The empty HTML element tags like "<div></div>" is a hack to get
;; around a Blackfriday limitation. Details:
;; https://github.com/kaushalmodi/ox-hugo/issues/93.

;;; ox-blackfriday.el ends here
9 changes: 5 additions & 4 deletions ox-hugo.el
Expand Up @@ -1781,7 +1781,7 @@ information."
;; 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))
(setq ret (org-blackfriday--div-wrap-maybe example-block 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 Expand Up @@ -2670,7 +2670,8 @@ communication channel."
(org-blackfriday--div-wrap-maybe
paragraph
(concat label
(org-hugo-paragraph--process-content paragraph contents info))))))))
(org-hugo-paragraph--process-content paragraph contents info))
info))))))

;;;; Source Blocks
(defun org-hugo-src-block (src-block _contents info)
Expand Down Expand Up @@ -2828,7 +2829,7 @@ channel."
(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))
(setq ret (org-blackfriday--div-wrap-maybe src-block ret info))
ret))))

;;;; Special Block
Expand Down Expand Up @@ -2915,7 +2916,7 @@ INFO is a plist holding export options."
(format "%s\n%s\n%s"
sc-begin contents sc-end)))
(t
(org-blackfriday-special-block special-block contents nil))))))
(org-blackfriday-special-block special-block contents info))))))



Expand Down
4 changes: 0 additions & 4 deletions test/site/content/posts/alert-short-code-lookalike.md
Expand Up @@ -139,7 +139,6 @@ div.alert-warning p:nth-child(2):before {
## Alert using Special Block {#alert-using-special-block}

<div class="alert-note alert">
<div></div>

Here's a tip or note.

Expand All @@ -148,7 +147,6 @@ This can be multi-paragraph too.
</div>

<div class="alert-warning alert">
<div></div>

Here's a warning!

Expand All @@ -162,14 +160,12 @@ This can be multi-paragraph too.
This will work only if the message is a single paragraph.

<div class="alert alert-note">
<div></div>

Here's a tip or note.

</div>

<div class="alert alert-warning">
<div></div>

Here's a warning!
</div>
2 changes: 0 additions & 2 deletions test/site/content/posts/center-align.md
Expand Up @@ -18,7 +18,6 @@ From [`C-h i g (org) Paragraphs`](https://orgmode.org/manual/Paragraphs.html):
<style>.org-center { margin-left: auto; margin-right: auto; text-align: center; }</style>

<div class="org-center">
<div></div>

Everything should be made as simple as possible, <br />
but not any simpler
Expand All @@ -31,7 +30,6 @@ but not any simpler
<style>.org-center { margin-left: auto; margin-right: auto; text-align: center; }</style>

<div class="org-center">
<div></div>

**bold** <br />
_italics_ <br />
Expand Down
4 changes: 0 additions & 4 deletions test/site/content/posts/citations-example-toml.md
Expand Up @@ -38,10 +38,6 @@ arbitrarily picked post: [Citation Linking]({{< relref "citation-linking" >}}).

<div class="foo">

<div>

</div>

**bold** *italics*

</div>
Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/example-blocks-with-attr-html.md
Expand Up @@ -9,7 +9,6 @@ Some text.
<style>.indent-block { padding-left: 50px; }</style>

<div class="indent-block">
<div></div>

```text
This is an example
Expand Down
4 changes: 0 additions & 4 deletions test/site/content/posts/lists-with-attr-html.md
Expand Up @@ -9,7 +9,6 @@ draft = false
<style>.red-text { color: red; }</style>

<div class="red-text">
<div></div>

- Red list item 1
- Red list item 2
Expand All @@ -20,7 +19,6 @@ draft = false
<style>.green-text { color: green; }</style>

<div class="green-text">
<div></div>

- Green list item 1
- Green list item 2
Expand All @@ -31,7 +29,6 @@ draft = false
## Ordered lists {#ordered-lists}

<div class="green-text">
<div></div>

1. Green ordered list item 1
2. Green ordered list item 2
Expand All @@ -55,7 +52,6 @@ _The `green-text` style is defined in the list above this one._
## Definition/descriptive lists {#definition-descriptive-lists}

<div class="red-text">
<div></div>

Defn A
: Something A in red
Expand Down
2 changes: 0 additions & 2 deletions test/site/content/posts/org-babel-results.md
Expand Up @@ -36,7 +36,6 @@ print(str[1:])
<style>.results-fixed-block { color: blue; }</style>

<div class="results-fixed-block">
<div></div>

```text
ef
Expand Down Expand Up @@ -77,7 +76,6 @@ echo "ABC\nDEF\nGHI\nJKL\nMNO\nPQR\nSTU\nVWX\nYZ0\n123\n456\n789"
<style>.results-example-block { color: green; }</style>

<div class="results-example-block">
<div></div>

```text
ABC
Expand Down
Expand Up @@ -68,7 +68,6 @@ not result in a new paragraph in HTML.
## Not a recognized paired shortcode {#not-a-recognized-paired-shortcode}

<div class="foo">
<div></div>

Content **with** Markdown _emphasis_ characters is rendered fine in the
default Special Blocks.
Expand Down
2 changes: 0 additions & 2 deletions test/site/content/posts/paragraphs-with-attr-html.md
Expand Up @@ -9,7 +9,6 @@ Regular text.
<style>.red-text { color: red; }</style>

<div class="red-text">
<div></div>

Red text.

Expand All @@ -20,7 +19,6 @@ Regular text.
<style>.green-text { color: green; }</style>

<div class="green-text">
<div></div>

Green text.

Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/quote-blocks-with-attr-html.md
Expand Up @@ -9,7 +9,6 @@ Some text.
<style>.red-text { color: red; }</style>

<div class="red-text">
<div></div>

> This is a red quote.
</div>
Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/source-blocks-with-attr-html.md
Expand Up @@ -9,7 +9,6 @@ Some text.
<style>.indent-block { padding-left: 50px; }</style>

<div class="indent-block">
<div></div>

```emacs-lisp
(message (mapconcat #'identity
Expand Down
1 change: 0 additions & 1 deletion test/site/content/posts/special-block-whitespace.md
Expand Up @@ -9,7 +9,6 @@ draft = false
+++

<div class="foo">
<div></div>



Expand Down

0 comments on commit 100010c

Please sign in to comment.