guides 0.6.8 → 0.6.8.1
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.
- data/lib/guides/generator.rb +1 -1
- data/lib/guides/indexer.rb +27 -27
- data/lib/guides/textile_transformer.rb +13 -7
- data/lib/guides/version.rb +1 -1
- data/spec/textile_transformer_spec.rb +13 -1
- metadata +1 -1
data/lib/guides/generator.rb
CHANGED
data/lib/guides/indexer.rb
CHANGED
@@ -6,10 +6,11 @@ module Guides
|
|
6
6
|
class Indexer
|
7
7
|
attr_reader :body, :result, :warnings, :level_hash
|
8
8
|
|
9
|
-
def initialize(body, warnings)
|
9
|
+
def initialize(body, warnings, production = false)
|
10
10
|
@body = body
|
11
11
|
@result = @body.dup
|
12
12
|
@warnings = warnings
|
13
|
+
@production = production
|
13
14
|
end
|
14
15
|
|
15
16
|
def index
|
@@ -19,41 +20,40 @@ module Guides
|
|
19
20
|
private
|
20
21
|
|
21
22
|
def process(string, current_level=3, counters=[1])
|
23
|
+
if @production
|
24
|
+
# Ignore anything in construction tags
|
25
|
+
string = string.gsub(%r{<construction>.*?</construction>}m, '')
|
26
|
+
end
|
27
|
+
|
22
28
|
s = StringScanner.new(string)
|
23
29
|
|
24
30
|
level_hash = ActiveSupport::OrderedHash.new
|
25
31
|
|
26
|
-
while
|
27
|
-
|
28
|
-
s.match?(re)
|
29
|
-
if matched = s.matched
|
30
|
-
matched =~ re
|
31
|
-
level, idx, title = $1.to_i, $2, $3.strip
|
32
|
+
while s.scan_until(%r{^h(\d)(?:\((#.*?)\))?\s*\.\s*(.*)$})
|
33
|
+
level, idx, title = s[1].to_i, s[2], s[3].strip
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
if level < current_level
|
36
|
+
# This is needed. Go figure.
|
37
|
+
return level_hash
|
38
|
+
elsif level == current_level
|
39
|
+
index = counters.join(".")
|
40
|
+
idx ||= '#' + title_to_idx(title)
|
39
41
|
|
40
|
-
|
42
|
+
raise "Parsing Fail" unless @result.sub!(s.matched, "h#{level}(#{idx}). #{index} #{title}")
|
41
43
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
key = {
|
45
|
+
:title => title,
|
46
|
+
:id => idx
|
47
|
+
}
|
48
|
+
# Recurse
|
49
|
+
counters << 1
|
50
|
+
level_hash[key] = process(s.post_match, current_level + 1, counters)
|
51
|
+
counters.pop
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
53
|
+
# Increment the current level
|
54
|
+
last = counters.pop
|
55
|
+
counters << last + 1
|
55
56
|
end
|
56
|
-
s.getch
|
57
57
|
end
|
58
58
|
level_hash
|
59
59
|
end
|
@@ -2,6 +2,14 @@ require "strscan"
|
|
2
2
|
require "cgi"
|
3
3
|
|
4
4
|
module Guides
|
5
|
+
module HTMLFormatter
|
6
|
+
include RedCloth::Formatters::HTML
|
7
|
+
|
8
|
+
def br(opts)
|
9
|
+
" "
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
class TextileTransformer
|
6
14
|
LANGUAGES = { "ruby" => "ruby", "sql" => "sql", "javascript" => "javascript",
|
7
15
|
"css" => "css", "plain" => "plain", "erb" => "ruby; html-script: true",
|
@@ -61,23 +69,21 @@ module Guides
|
|
61
69
|
end
|
62
70
|
|
63
71
|
def consume_note(css_class)
|
64
|
-
match = scan_until /(\r?\n){2}/
|
72
|
+
match = scan_until /(\r?\n){2,}/ # We need at least 2 line breaks but we want to match as many as exist
|
65
73
|
note = match.pre_match.gsub(/\n\s*/, " ")
|
66
74
|
note = RedCloth.new(note, [:lite_mode]).to_html
|
67
75
|
@output << %{<div class="#{css_class}"><p>#{note}</p></div>\n}
|
68
76
|
end
|
69
77
|
|
70
78
|
def consume_construction
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
@string.sub!(%r{</construction>}, '')
|
79
|
+
match = scan_until(%r{</construction>})
|
80
|
+
unless @production
|
81
|
+
@string = match.pre_match + @string
|
75
82
|
end
|
76
83
|
end
|
77
84
|
|
78
85
|
def flush_textile
|
79
|
-
@
|
80
|
-
@output << RedCloth.new(@pending_textile).to_html << "\n"
|
86
|
+
@output << RedCloth.new(@pending_textile).to(HTMLFormatter) << "\n"
|
81
87
|
@pending_textile = ""
|
82
88
|
end
|
83
89
|
end
|
data/lib/guides/version.rb
CHANGED
@@ -64,8 +64,20 @@ describe "Transformer" do
|
|
64
64
|
@transformer.transform("This\nhas\nbreaks.\n\nAnd\nparagraphs.").should == "<p>This has breaks.</p>\n<p>And paragraphs.</p>\n"
|
65
65
|
end
|
66
66
|
|
67
|
+
it "properly handles more than two line breaks" do
|
68
|
+
result = @transformer.transform("TIP: With \"link\":file.html.\n\n\nh4. Heading")
|
69
|
+
result.should == %{\n<div class="info"><p>With <a href="file.html">link</a>.</p></div>\n<h4>Heading</h4>\n}
|
70
|
+
|
71
|
+
result = @transformer.transform("Test\n\n\nh4. Heading")
|
72
|
+
result.should =="<p>Test</p>\n<h4>Heading</h4>\n"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "handles single line returns outside of paragraphs" do
|
76
|
+
@transformer.transform("* One\n* Two\n* Three").should == "<ul>\n\t<li>One</li>\n\t<li>Two</li>\n\t<li>Three</li>\n</ul>\n"
|
77
|
+
end
|
78
|
+
|
67
79
|
it "handles <construction>" do
|
68
|
-
str = "Testing this out. <construction>Write more
|
80
|
+
str = "Testing this out. <construction>Write more\nhere later.</construction> This is awesome.\n"
|
69
81
|
@transformer.transform(str).should == "<p>Testing this out. This is awesome.</p>\n"
|
70
82
|
@dev_transformer.transform(str).should == "<p>Testing this out. Write more here later. This is awesome.</p>\n"
|
71
83
|
|