literate_md 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/literate_md +20 -6
- data/test/expected_test.html +49 -0
- data/test/expected_test.rb +4 -4
- data/test/test.md +2 -2
- data/test/test_literate_md.rb +19 -9
- metadata +2 -1
data/bin/literate_md
CHANGED
@@ -29,12 +29,13 @@ $ext_for_lang = {
|
|
29
29
|
|
30
30
|
class Weave < Redcarpet::Render::XHTML
|
31
31
|
attr_accessor :default_lang, :original_text, :standalone
|
32
|
+
def initialize; super; @tokens = Hash.new end
|
32
33
|
def block_code(code, lang)
|
33
34
|
l_ang, r_ang, equiv = "__lang__", "__rang__", "__equiv__"
|
34
35
|
line_num = @original_text[0,@original_text.index(code)].count("\n")+1
|
35
36
|
code = code.
|
36
|
-
gsub(/@(#{$allowed})@\s
|
37
|
-
gsub(/@(#{$allowed})
|
37
|
+
gsub(/@(#{$allowed})@\s*\+=/) {|match| "#{l_ang}" + to_token(match) + "#{r_ang}+#{equiv}"}.
|
38
|
+
gsub(/@(#{$allowed})@/) {|match| "#{l_ang}" + to_token(match) + "#{r_ang}"}
|
38
39
|
code = CodeRay.
|
39
40
|
scan(code, lang.nil? ? @default_lang : lang.to_sym).
|
40
41
|
html(
|
@@ -42,13 +43,26 @@ class Weave < Redcarpet::Render::XHTML
|
|
42
43
|
:css => :style,
|
43
44
|
:line_numbers => :table,
|
44
45
|
:line_number_start => line_num)
|
45
|
-
code.
|
46
|
-
|
47
|
-
|
46
|
+
code.gsub!(/#{l_ang}/,'⟨')
|
47
|
+
code.gsub!(/#{r_ang}/,'⟩')
|
48
|
+
code.gsub!(/#{equiv}/,'≡')
|
49
|
+
@tokens.each_pair {|k,v| code.gsub! v, k }
|
50
|
+
code
|
48
51
|
end
|
49
52
|
def codespan(code); block_code(code,nil) end
|
50
|
-
def doc_header
|
53
|
+
def doc_header
|
54
|
+
@original_text.gsub!(/\t/,' ') # redcarpet converts tabs somewhere
|
55
|
+
@standalone ? "<html><body>" : nil
|
56
|
+
end
|
51
57
|
def doc_footer; "</body></html>" if @standalone end
|
58
|
+
def to_token code
|
59
|
+
code = code[1, code.rindex('@')-1]
|
60
|
+
if @tokens.has_key? code
|
61
|
+
@tokens[code]
|
62
|
+
else
|
63
|
+
@tokens[code] = "__token#{@tokens.size}__"
|
64
|
+
end
|
65
|
+
end
|
52
66
|
end
|
53
67
|
|
54
68
|
def write_to path, data
|
@@ -0,0 +1,49 @@
|
|
1
|
+
<html><body>
|
2
|
+
<h3>my file</h3>
|
3
|
+
|
4
|
+
<p>add some <em>text</em>:</p>
|
5
|
+
<table class="CodeRay"><tr>
|
6
|
+
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n5" name="n5">5</a>
|
7
|
+
<a href="#n6" name="n6">6</a>
|
8
|
+
<a href="#n7" name="n7">7</a>
|
9
|
+
<a href="#n8" name="n8">8</a>
|
10
|
+
<a href="#n9" name="n9">9</a>
|
11
|
+
<strong><a href="#n10" name="n10">10</a></strong>
|
12
|
+
</pre></td>
|
13
|
+
<td class="code"><pre>⟨*⟩+≡
|
14
|
+
int main() {
|
15
|
+
⟨Do Stuff⟩
|
16
|
+
⟨Do More Stuff⟩
|
17
|
+
<span style="color:#080;font-weight:bold">return</span> <span style="color:#00D">0</span>;
|
18
|
+
}
|
19
|
+
</pre></td>
|
20
|
+
</tr></table>
|
21
|
+
|
22
|
+
<p>and some more text. we should probably say what dostuff is; it's:</p>
|
23
|
+
<table class="CodeRay"><tr>
|
24
|
+
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n16" name="n16">16</a>
|
25
|
+
<a href="#n17" name="n17">17</a>
|
26
|
+
<a href="#n18" name="n18">18</a>
|
27
|
+
<a href="#n19" name="n19">19</a>
|
28
|
+
<strong><a href="#n20" name="n20">20</a></strong>
|
29
|
+
<a href="#n21" name="n21">21</a>
|
30
|
+
<a href="#n22" name="n22">22</a>
|
31
|
+
</pre></td>
|
32
|
+
<td class="code"><pre>⟨Do Stuff⟩+≡
|
33
|
+
int* me = &<span style="color:#00D">1</span>;
|
34
|
+
*me++;
|
35
|
+
⟨Do More Stuff⟩+≡
|
36
|
+
<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#808">* MORE! MORE! *</span><span style="color:#404">/</span></span>
|
37
|
+
⟨Do Stuff⟩+≡
|
38
|
+
<span style="background-color:hsla(300,100%,50%,0.06)"><span style="color:#404">/</span><span style="color:#404">/</span></span> again!
|
39
|
+
</pre></td>
|
40
|
+
</tr></table>
|
41
|
+
|
42
|
+
<p>was that clearer? More stuff is the same as the first:</p>
|
43
|
+
<table class="CodeRay"><tr>
|
44
|
+
<td class="line-numbers" title="double click to toggle" ondblclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre><a href="#n28" name="n28">28</a>
|
45
|
+
</pre></td>
|
46
|
+
<td class="code"><pre>⟨Do More Stuff⟩+≡ ⟨Do Stuff⟩
|
47
|
+
</pre></td>
|
48
|
+
</tr></table>
|
49
|
+
</body></html>
|
data/test/expected_test.rb
CHANGED
data/test/test.md
CHANGED
data/test/test_literate_md.rb
CHANGED
@@ -2,23 +2,33 @@ require 'test/unit'
|
|
2
2
|
require 'ostruct'
|
3
3
|
|
4
4
|
class TestLiterateMD < Test::Unit::TestCase
|
5
|
+
def run_and_verify opts, actual_file, expected_file
|
6
|
+
tidy_up actual_file
|
7
|
+
$opts = opts
|
8
|
+
run_it
|
9
|
+
expected = File.open(expected_file, 'r'){|f|f.read}
|
10
|
+
actual = File.open(actual_file, 'r'){|f|f.read}
|
11
|
+
assert_equal expected, actual
|
12
|
+
tidy_up actual_file
|
13
|
+
end
|
14
|
+
|
5
15
|
def test_tangle
|
6
|
-
|
7
|
-
$opts = OpenStruct.new(
|
16
|
+
run_and_verify OpenStruct.new(
|
8
17
|
:tangle => true,
|
9
18
|
:weave => false,
|
10
19
|
:outputdir => '.',
|
11
20
|
:files => 'test/test.md',
|
12
|
-
:lang => 'ruby')
|
13
|
-
run_it
|
14
|
-
expected = File.open('test/expected_test.rb', 'r'){|f|f.read}
|
15
|
-
actual = File.open(actual_file, 'r'){|f|f.read}
|
16
|
-
assert_equal expected, actual
|
17
|
-
tidy_up actual_file
|
21
|
+
:lang => 'ruby'), 'test/test.rb', 'test/expected_test.rb'
|
18
22
|
end
|
19
23
|
|
20
24
|
def test_weave
|
21
|
-
|
25
|
+
run_and_verify OpenStruct.new(
|
26
|
+
:tangle => false,
|
27
|
+
:weave => true,
|
28
|
+
:standalone => true,
|
29
|
+
:outputdir => '.',
|
30
|
+
:files => 'test/test.md',
|
31
|
+
:lang => 'ruby'), 'test.md.html', 'test/expected_test.html'
|
22
32
|
end
|
23
33
|
|
24
34
|
def tidy_up file
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: literate_md
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- remis
|
@@ -67,6 +67,7 @@ extra_rdoc_files: []
|
|
67
67
|
files:
|
68
68
|
- licenses/LICENSE-2.0.txt
|
69
69
|
- bin/literate_md
|
70
|
+
- test/expected_test.html
|
70
71
|
- test/expected_test.rb
|
71
72
|
- test/test.md
|
72
73
|
- test/test_literate_md.rb
|