Skip to content

Commit

Permalink
Fix exporting #+toc keyword with 'local'
Browse files Browse the repository at this point in the history
Fixes #183.
  • Loading branch information
kaushalmodi committed Aug 7, 2018
1 parent 850c10d commit 03fe85a
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ox-hugo.el
Expand Up @@ -1025,12 +1025,21 @@ When optional argument LOCAL is non-nil, build a table of
contents according to the current headline."
(let* ((toc-headline
(unless local
(format "\n<div class=\"heading\">%s</div>\n\n"
(format "\n<div class=\"heading\">%s</div>\n"
(org-html--translate "Table of Contents" info))))
(current-level nil)
(toc-items
(mapconcat
(lambda (headline)
(let* ((level (org-export-get-relative-level headline info))
(let* ((level-raw (org-export-get-relative-level headline info))
(current-level-inner (progn
(unless current-level
(setq current-level level-raw))
current-level))
(relative-level (1+ (- level-raw current-level-inner)))
(level (if local
relative-level
level-raw))
(indentation (make-string (* 4 (1- level)) ?\s))
(todo (and (org-hugo--plist-get-true-p info :with-todo-keywords)
(org-element-property :todo-keyword headline)))
Expand Down Expand Up @@ -1060,7 +1069,10 @@ contents according to the current headline."
(and tags
(format ":%s:"
(mapconcat #'identity tags ":")))))))
;; (message "[ox-hugo build-toc DBG] current-level-inner:%d relative-level:%d"
;; current-level-inner relative-level)
;; (message "[ox-hugo build-toc DBG] level:%d, number:%s" level number)
;; (message "[ox-hugo build-toc DBG] indentation: %S" indentation)
;; (message "[ox-hugo build-toc DBG] todo: %s | %s" todo todo-str)
(concat indentation "- " number toc-entry tags)))
(org-export-collect-headlines info n (and local keyword))
Expand All @@ -1080,9 +1092,13 @@ contents according to the current headline."
" list-style: none;\n"
" }\n"
"</style>\n"))
"<div class=\"ox-hugo-toc toc\">\n" ;This is a nasty workaround
"<div></div>\n" ;till Hugo/Blackfriday support
toc-headline ;wrapping Markdown in HTML div's.
(format "<div class=\"ox-hugo-toc toc%s\">\n"
(if local
(format " local level-%d" current-level)
""))
"<div></div>\n" ;This is a nasty workaround till Hugo/Blackfriday support
toc-headline ;wrapping Markdown in HTML div's.
"\n"
toc-items ;https://github.com/kaushalmodi/ox-hugo/issues/93
"\n\n"
"</div>\n"
Expand Down
30 changes: 30 additions & 0 deletions test/site/content-org/all-posts.org
Expand Up @@ -4620,6 +4620,36 @@ This post should also be created in =content/section-a/=.
:END:
#+toc: headlines 6
#+include: "./all-posts.org::#nested-sections-example" :only-contents t
*** TOC Local :local:
:PROPERTIES:
:EXPORT_FILE_NAME: toc-local
:END:
Below, TOC is exported with only level-1 headings in this post.
#+toc: headlines 1
**** Post sub-heading 1
Below, TOC is exported with only level-1 headings *relative to* this
"Post sub-heading 1" section.
#+toc: headlines 1 local
***** Post sub-heading 1.1
****** Post sub-heading 1.1.1
***** Post sub-heading 1.2
***** Post sub-heading 1.3
**** Post sub-heading 2
***** Post sub-heading 2.1
***** Post sub-heading 2.2
Below, TOC is exported with only up to level-2 headings *relative to*
this "Post sub-heading 2.2" section.
#+toc: headlines 2 local
****** Post sub-heading 2.2.1
****** Post sub-heading 2.2.2
****** Post sub-heading 2.2.3
Below, TOC is exported with only level-1 headings *relative to* this
"Post sub-heading 2.2.3" section.
#+toc: headlines 1 local
******* Post sub-heading 2.2.3.1
******* Post sub-heading 2.2.3.2
**** Post sub-heading 3
***** Post sub-heading 3.1
* Pandoc Citations :pandoc:citations:
:PROPERTIES:
:EXPORT_HUGO_PANDOC_CITATIONS: t
Expand Down
104 changes: 104 additions & 0 deletions test/site/content/posts/toc-local.md
@@ -0,0 +1,104 @@
+++
title = "TOC Local"
tags = ["keyword", "toc", "local"]
draft = false
+++

Below, TOC is exported with only level-1 headings in this post.

<div class="ox-hugo-toc toc">
<div></div>

<div class="heading">Table of Contents</div>

- [Post sub-heading 1](#post-sub-heading-1)
- [Post sub-heading 2](#post-sub-heading-2)
- [Post sub-heading 3](#post-sub-heading-3)

</div>
<!--endtoc-->


## Post sub-heading 1 {#post-sub-heading-1}

Below, TOC is exported with only level-1 headings **relative to** this
"Post sub-heading 1" section.

<div class="ox-hugo-toc toc local level-2">
<div></div>

- [Post sub-heading 1.1](#post-sub-heading-1-dot-1)
- [Post sub-heading 1.2](#post-sub-heading-1-dot-2)
- [Post sub-heading 1.3](#post-sub-heading-1-dot-3)

</div>
<!--endtoc-->


### Post sub-heading 1.1 {#post-sub-heading-1-dot-1}


#### Post sub-heading 1.1.1 {#post-sub-heading-1-dot-1-dot-1}


### Post sub-heading 1.2 {#post-sub-heading-1-dot-2}


### Post sub-heading 1.3 {#post-sub-heading-1-dot-3}


## Post sub-heading 2 {#post-sub-heading-2}


### Post sub-heading 2.1 {#post-sub-heading-2-dot-1}


### Post sub-heading 2.2 {#post-sub-heading-2-dot-2}

Below, TOC is exported with only up to level-2 headings **relative to**
this "Post sub-heading 2.2" section.

<div class="ox-hugo-toc toc local level-3">
<div></div>

- [Post sub-heading 2.2.1](#post-sub-heading-2-dot-2-dot-1)
- [Post sub-heading 2.2.2](#post-sub-heading-2-dot-2-dot-2)
- [Post sub-heading 2.2.3](#post-sub-heading-2-dot-2-dot-3)
- [Post sub-heading 2.2.3.1](#post-sub-heading-2-dot-2-dot-3-dot-1)
- [Post sub-heading 2.2.3.2](#post-sub-heading-2-dot-2-dot-3-dot-2)

</div>
<!--endtoc-->


#### Post sub-heading 2.2.1 {#post-sub-heading-2-dot-2-dot-1}


#### Post sub-heading 2.2.2 {#post-sub-heading-2-dot-2-dot-2}


#### Post sub-heading 2.2.3 {#post-sub-heading-2-dot-2-dot-3}

Below, TOC is exported with only level-1 headings **relative to** this
"Post sub-heading 2.2.3" section.

<div class="ox-hugo-toc toc local level-4">
<div></div>

- [Post sub-heading 2.2.3.1](#post-sub-heading-2-dot-2-dot-3-dot-1)
- [Post sub-heading 2.2.3.2](#post-sub-heading-2-dot-2-dot-3-dot-2)

</div>
<!--endtoc-->


##### Post sub-heading 2.2.3.1 {#post-sub-heading-2-dot-2-dot-3-dot-1}


##### Post sub-heading 2.2.3.2 {#post-sub-heading-2-dot-2-dot-3-dot-2}


## Post sub-heading 3 {#post-sub-heading-3}


### Post sub-heading 3.1 {#post-sub-heading-3-dot-1}

2 comments on commit 03fe85a

@kaushalmodi
Copy link
Owner Author

@kaushalmodi kaushalmodi commented on 03fe85a Aug 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vvsagar Your Gitlab site repo revealed a bug in ox-hugo; I had never tested #+toc: headlines N local using ox-hugo. That was buggy, and now fixed.

@vvsagar
Copy link

@vvsagar vvsagar commented on 03fe85a Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to be helpful!

Please sign in to comment.