madness 0.8.4 → 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/madness/document.rb +24 -46
- data/lib/madness/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ffaa41eacda31f502ff69d9c68189533cff31f293907089288b496e82038304
|
4
|
+
data.tar.gz: 44053382dd7dda362a34dd57e9663b7e9d46d510ca5cdecef4f1625468b8c376
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33dc087ef4dec3a0f79019846bd4aadf6b6eab4dd81ed9049d4d619c89bed23235de14879a99296e264dbb14aa87102b841f7a9a1d2f600ab7828d82286a817c
|
7
|
+
data.tar.gz: 74c35835a66aa14e44e9885c759445c64b82b8801bc8663c1d088d3d7fa123044d66331068200c649a943545ee8377a75ac038329d6feae4be5f5d92a6f1f1f7
|
data/lib/madness/document.rb
CHANGED
@@ -8,36 +8,13 @@ module Madness
|
|
8
8
|
include ServerHelper
|
9
9
|
using StringRefinements
|
10
10
|
|
11
|
-
attr_reader :base, :path
|
11
|
+
attr_reader :base, :path, :type, :file, :dir, :title
|
12
12
|
|
13
13
|
def initialize(path)
|
14
14
|
@path = path
|
15
15
|
@base = path.empty? ? docroot : "#{docroot}/#{path}"
|
16
16
|
@base.chomp! '/'
|
17
|
-
|
18
|
-
|
19
|
-
# Return :readme, :file or :empty
|
20
|
-
def type
|
21
|
-
set_base_attributes unless @type
|
22
|
-
@type
|
23
|
-
end
|
24
|
-
|
25
|
-
# Return the path to the actual markdown file
|
26
|
-
def file
|
27
|
-
set_base_attributes unless @file
|
28
|
-
@file
|
29
|
-
end
|
30
|
-
|
31
|
-
# Return the path to the document directory
|
32
|
-
def dir
|
33
|
-
set_base_attributes unless @dir
|
34
|
-
@dir
|
35
|
-
end
|
36
|
-
|
37
|
-
# Return the path to the document directory
|
38
|
-
def title
|
39
|
-
set_base_attributes unless @title
|
40
|
-
@title
|
17
|
+
set_base_attributes
|
41
18
|
end
|
42
19
|
|
43
20
|
# Return the HTML for that document
|
@@ -92,23 +69,25 @@ module Madness
|
|
92
69
|
@markdown ||= File.read file
|
93
70
|
end
|
94
71
|
|
72
|
+
def doc
|
73
|
+
@doc ||= CommonMarker.render_doc markdown, :DEFAULT, [:table]
|
74
|
+
end
|
75
|
+
|
95
76
|
# Convert markdown to HTML, with some additional processing:
|
96
77
|
# 1. Add anchors to headers
|
97
78
|
# 2. Syntax highilghting
|
98
79
|
# 3. Prepend H1 if needed
|
99
80
|
def markdown_to_html
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
add_anchor_ids doc
|
81
|
+
replace_toc_marker
|
82
|
+
prepend_h1 if config.auto_h1
|
83
|
+
add_anchor_ids
|
104
84
|
html = doc.to_html :UNSAFE
|
105
85
|
html = syntax_highlight(html) if config.highlighter
|
106
|
-
html = prepend_h1(html) if config.auto_h1
|
107
86
|
html
|
108
87
|
end
|
109
88
|
|
110
89
|
# Add anchors with IDs before all headers
|
111
|
-
def add_anchor_ids
|
90
|
+
def add_anchor_ids
|
112
91
|
doc.walk do |node|
|
113
92
|
if node.type == :header
|
114
93
|
anchor = CommonMarker::Node.new(:inline_html)
|
@@ -123,39 +102,38 @@ module Madness
|
|
123
102
|
end
|
124
103
|
|
125
104
|
# Replace <!-- TOC --> with a Table of Contents for the page
|
126
|
-
def replace_toc_marker
|
105
|
+
def replace_toc_marker
|
127
106
|
toc_marker = doc.find do |node|
|
128
107
|
node.type == :html and node.string_content.include? "<!-- TOC -->"
|
129
108
|
end
|
130
109
|
|
131
110
|
return unless toc_marker
|
132
111
|
|
112
|
+
toc_marker.insert_after document_toc
|
113
|
+
toc_marker.insert_after CommonMarker.render_doc("## Table of Contents").first_child
|
114
|
+
end
|
115
|
+
|
116
|
+
# Returns a UL object containing the document table of contents
|
117
|
+
def document_toc
|
133
118
|
toc = []
|
134
119
|
doc.walk do |node|
|
135
120
|
next unless node.type == :header
|
136
121
|
level = node.header_level
|
137
122
|
next unless level.between? 2, 3
|
138
123
|
text = node.first_child.string_content
|
139
|
-
|
140
|
-
|
141
|
-
toc << "- [#{text}](##{text.to_slug})"
|
142
|
-
else
|
143
|
-
toc << " - [#{text}](##{text.to_slug})"
|
144
|
-
end
|
124
|
+
spacer = " " * (level - 1)
|
125
|
+
toc << "#{spacer}- [#{text}](##{text.to_slug})"
|
145
126
|
end
|
146
127
|
|
147
128
|
toc = toc.join "\n"
|
148
|
-
|
149
|
-
toc_marker.insert_after toc.first_child
|
150
|
-
toc_marker.insert_after CommonMarker.render_doc("## Table of Contents").first_child
|
129
|
+
CommonMarker.render_doc(toc).first_child
|
151
130
|
end
|
152
131
|
|
153
132
|
# If the document does not start with an H1 tag, add it.
|
154
|
-
def prepend_h1
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
html
|
133
|
+
def prepend_h1
|
134
|
+
return if doc.first_child.type == :header and doc.first_child.header_level == 1
|
135
|
+
h1 = CommonMarker.render_doc("# #{title}").first_child
|
136
|
+
doc.first_child.insert_before h1
|
159
137
|
end
|
160
138
|
|
161
139
|
# Apply syntax highlighting with CodeRay. This will parse for any
|
data/lib/madness/version.rb
CHANGED
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.
|
4
|
+
version: 0.8.5
|
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-
|
11
|
+
date: 2020-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coderay
|
@@ -128,14 +128,14 @@ dependencies:
|
|
128
128
|
requirements:
|
129
129
|
- - "~>"
|
130
130
|
- !ruby/object:Gem::Version
|
131
|
-
version: '
|
131
|
+
version: '5.1'
|
132
132
|
type: :runtime
|
133
133
|
prerelease: false
|
134
134
|
version_requirements: !ruby/object:Gem::Requirement
|
135
135
|
requirements:
|
136
136
|
- - "~>"
|
137
137
|
- !ruby/object:Gem::Version
|
138
|
-
version: '
|
138
|
+
version: '5.1'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: rack-ssl
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|