madness 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 177c0fe6c496b8b07eed2b80140a40db946c8a5af01a1e3d42655e652f3c50b2
4
- data.tar.gz: 4e351e74d0a5df4d0111e02ebbe070a4c06cff2fb615b38ed7ef3f1407d3c7fd
3
+ metadata.gz: fe93b51cce9d5f1e2b37597b86bbcab62273fd2ba588d013308b194bb607d20e
4
+ data.tar.gz: 4b5e8a9de2af92f7aef8fa86792c8f3156ae9aba4b5049507693c2467dcc439c
5
5
  SHA512:
6
- metadata.gz: 875ff85eb026afc5146b56dfd213ec5943ff337847d1a0c4f131963f3350c0a8220b516290b4bd56015e4d59b009bb43445f03a323fda03a97bf28cb4baf4d5f
7
- data.tar.gz: 8d6dd211a8b35b1a70b74adcf02b178876631fc55d19d07b409947a57420fcdb11b89f1b2ecf7d3a111203e9bdacc3ffefd75d126b45a791d7bbaf44b0bf62ee
6
+ metadata.gz: 78e416333ece7eb2a236a07593c31f78cb93919f8842b5fede75baff6ae6e07d956ff0b1f30501c1be99416aab4ce036885d49800eeea5d38d5f811745d4c55d
7
+ data.tar.gz: d237b707b9a7d7a6d9334942a760811f51463a9aa04c007f54789bfa89be08cd04ac6bf7e6f946d86aa45302b50d3d6e4e7c5d4d83ffec88fb4a42c65cdb063c
data/README.md CHANGED
@@ -61,7 +61,7 @@ searching for local, markdown based documentation directories.
61
61
  - Configure with a configuration file or command arguments.
62
62
  - Fully customizable theme.
63
63
  - Automatic generation of navigation sidebar.
64
- - Automatic generation of Table of Contents.
64
+ - Automatic generation of Table of Contents (site-wide and inline).
65
65
 
66
66
  ## Usage
67
67
 
@@ -179,8 +179,17 @@ will be automatically added based on the file name.
179
179
 
180
180
  ## Table of Contents Generation
181
181
 
182
- You can use the `madness --toc FILENAME` command to generate a
183
- "Table of Contents" markdown file.
182
+ ### Site-wide
183
+
184
+ To generate a Table of Contents file for the entire site (for the directories
185
+ and files), run `madness --toc FILENAME`
186
+
187
+ ### Inline
188
+
189
+ If you have long markdown documents, and you wish to add an inline Table of
190
+ Contents for them, simply add an HTML comment `<!-- TOC -->` where you want
191
+ the Table of Contents to be generated. The inserted list will only consider
192
+ H2 and H3 headings.
184
193
 
185
194
  ## Hidden Directories
186
195
 
@@ -1,4 +1,7 @@
1
- require 'byebug' if ENV['BYEBUG']
1
+ if ENV['BYEBUG']
2
+ require 'byebug'
3
+ require 'lp'
4
+ end
2
5
 
3
6
  require 'requires'
4
7
 
@@ -99,6 +99,7 @@ module Madness
99
99
  def markdown_to_html
100
100
  doc = CommonMarker.render_doc markdown, :DEFAULT, [:table]
101
101
 
102
+ replace_toc_marker doc
102
103
  add_anchor_ids doc
103
104
  html = doc.to_html :UNSAFE
104
105
  html = syntax_highlight(html) if config.highlighter
@@ -121,6 +122,34 @@ module Madness
121
122
  end
122
123
  end
123
124
 
125
+ # Replace <!-- TOC --> with a Table of Contents for the page
126
+ def replace_toc_marker(doc)
127
+ toc_marker = doc.find do |node|
128
+ node.type == :html and node.string_content.include? "<!-- TOC -->"
129
+ end
130
+
131
+ return unless toc_marker
132
+
133
+ toc = []
134
+ doc.walk do |node|
135
+ next unless node.type == :header
136
+ level = node.header_level
137
+ next unless level.between? 2, 3
138
+ text = node.first_child.string_content
139
+
140
+ if level == 2
141
+ toc << "- [#{text}](##{text.to_slug})"
142
+ else
143
+ toc << " - [#{text}](##{text.to_slug})"
144
+ end
145
+ end
146
+
147
+ toc = toc.join "\n"
148
+ toc = CommonMarker.render_doc toc
149
+ toc_marker.insert_after toc.first_child
150
+ toc_marker.insert_after CommonMarker.render_doc("## Table of Contents").first_child
151
+ end
152
+
124
153
  # If the document does not start with an H1 tag, add it.
125
154
  def prepend_h1(html)
126
155
  unless html[0..3] == "<h1>"
@@ -5,7 +5,7 @@ module Madness
5
5
 
6
6
  attr_reader :dir
7
7
 
8
- def initialize(dir=nil)
8
+ def initialize(dir = nil)
9
9
  @dir = dir || docroot
10
10
  end
11
11
 
@@ -20,7 +20,7 @@ module Madness
20
20
 
21
21
  private
22
22
 
23
- def toc!(path=dir, indent=0)
23
+ def toc!(path = dir, indent = 0)
24
24
  list = Directory.new(path).list
25
25
 
26
26
  result = []
@@ -1,3 +1,3 @@
1
1
  module Madness
2
- VERSION = "0.8.3"
2
+ VERSION = "0.8.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-06 00:00:00.000000000 Z
11
+ date: 2020-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coderay
@@ -327,7 +327,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
327
327
  - !ruby/object:Gem::Version
328
328
  version: '0'
329
329
  requirements: []
330
- rubygems_version: 3.1.2
330
+ rubygems_version: 3.1.4
331
331
  signing_key:
332
332
  specification_version: 4
333
333
  summary: Instant Markdown Server