dolt 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dolt/disk_repo_resolver.rb +3 -3
- data/lib/dolt/version.rb +1 -1
- data/lib/dolt/view/markup.rb +70 -0
- data/test/dolt/view/markup_test.rb +64 -0
- data/vendor/ui/css/gitorious.css +7 -0
- metadata +3 -3
data/lib/dolt/version.rb
CHANGED
data/lib/dolt/view/markup.rb
CHANGED
@@ -21,6 +21,7 @@ module Dolt
|
|
21
21
|
module View
|
22
22
|
module Markup
|
23
23
|
def render_markup(path, content)
|
24
|
+
content = highlight_code_blocks(path, content)
|
24
25
|
markup = GitHub::Markup.render(path, content)
|
25
26
|
"<div class=\"gts-markup\">#{markup}</div>"
|
26
27
|
end
|
@@ -32,6 +33,75 @@ module Dolt
|
|
32
33
|
def format_text_blob(path, code, repo = nil, ref = nil)
|
33
34
|
render_markup(path, code)
|
34
35
|
end
|
36
|
+
|
37
|
+
def highlight_code_blocks(path, markup)
|
38
|
+
return markup unless path =~ /\.(md|mkdn?|mdwn|mdown|markdown)$/
|
39
|
+
can_highlight = respond_to?(:highlight)
|
40
|
+
CodeBlockParser.parse(markup) do |lexer, code|
|
41
|
+
code = can_highlight ? highlight(path, code, { :lexer => lexer }) : code
|
42
|
+
l = can_highlight ? Pygments::Lexer.find(lexer) : nil
|
43
|
+
"<pre class=\"#{l && l.aliases.first} prettyprint\">#{code}</pre>"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class CodeBlockParser
|
49
|
+
attr_reader :lines
|
50
|
+
|
51
|
+
def self.parse(markup, &block)
|
52
|
+
new(markup).parse(&block)
|
53
|
+
end
|
54
|
+
|
55
|
+
def initialize(markup)
|
56
|
+
@lines = markup.split("\n")
|
57
|
+
@current_code_bock = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse(&block)
|
61
|
+
result = []
|
62
|
+
|
63
|
+
while line = @lines.shift
|
64
|
+
if closes_code_block?(line)
|
65
|
+
result << block.call(*close_active_code_block)
|
66
|
+
elsif active_code_block?
|
67
|
+
append_active_code_block(line)
|
68
|
+
elsif starts_code_block?(line)
|
69
|
+
start_code_block(line)
|
70
|
+
else
|
71
|
+
result << line
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
result.join("\n")
|
76
|
+
end
|
77
|
+
|
78
|
+
def active_code_block?
|
79
|
+
!@current_code_bock.nil?
|
80
|
+
end
|
81
|
+
|
82
|
+
def starts_code_block?(line)
|
83
|
+
line.match(/^```.*/)
|
84
|
+
end
|
85
|
+
|
86
|
+
def closes_code_block?(line)
|
87
|
+
active_code_block? && line == "```"
|
88
|
+
end
|
89
|
+
|
90
|
+
def start_code_block(line)
|
91
|
+
m = line.match(/```([^\s]+)/)
|
92
|
+
@current_code_bock = [m && m[1], []]
|
93
|
+
end
|
94
|
+
|
95
|
+
def append_active_code_block(line)
|
96
|
+
@current_code_bock[1] << line
|
97
|
+
end
|
98
|
+
|
99
|
+
def close_active_code_block
|
100
|
+
lexer = @current_code_bock[0]
|
101
|
+
code = @current_code_bock[1].join("\n")
|
102
|
+
@current_code_bock = nil
|
103
|
+
[lexer, code]
|
104
|
+
end
|
35
105
|
end
|
36
106
|
end
|
37
107
|
end
|
@@ -17,14 +17,78 @@
|
|
17
17
|
#++
|
18
18
|
require "test_helper"
|
19
19
|
require "dolt/view/markup"
|
20
|
+
require "dolt/view/syntax_highlight"
|
21
|
+
|
22
|
+
module StubHighlighter
|
23
|
+
def highlight(path, code, opt = {})
|
24
|
+
return "##[#{opt[:lexer]}]#{code}##"
|
25
|
+
end
|
26
|
+
end
|
20
27
|
|
21
28
|
describe Dolt::View::Markup do
|
22
29
|
include Dolt::View::Markup
|
23
30
|
|
24
31
|
describe "#render_markup" do
|
32
|
+
include Dolt::View::SyntaxHighlight
|
33
|
+
|
25
34
|
it "wraps markup in .gts-markup" do
|
26
35
|
html = render_markup("file.md", "# Hey")
|
27
36
|
assert_match "<div class=\"gts-markup\">", html
|
28
37
|
end
|
38
|
+
|
39
|
+
it "renders multi-line code blocks with syntax highlighting" do
|
40
|
+
html = render_markup("file.md", <<-MD)
|
41
|
+
```cl
|
42
|
+
(s-trim-left "trim ") ;; => "trim "
|
43
|
+
(s-trim-left " this") ;; => "this"
|
44
|
+
```
|
45
|
+
MD
|
46
|
+
|
47
|
+
assert_match "<pre class=\"common-lisp prettyprint\">", html
|
48
|
+
end
|
49
|
+
|
50
|
+
it "highlights multiple separate multi-line code blocks" do
|
51
|
+
html = render_markup("file.md", <<-MD)
|
52
|
+
# # This stuff
|
53
|
+
|
54
|
+
```cl
|
55
|
+
(s-trim-left "trim ") ;; => "trim "
|
56
|
+
(s-trim-left " this") ;; => "this"
|
57
|
+
```
|
58
|
+
|
59
|
+
# And this stuff
|
60
|
+
|
61
|
+
```cl
|
62
|
+
(s-trim-left "trim ") ;; => "trim "
|
63
|
+
(s-trim-left " this") ;; => "this"
|
64
|
+
```
|
65
|
+
MD
|
66
|
+
|
67
|
+
assert_equal 2, html.scan(/common-lisp/).length
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#highlight_code_blocks" do
|
72
|
+
include StubHighlighter
|
73
|
+
|
74
|
+
it "does not touch non-markdown files" do
|
75
|
+
content = "```cl\n(yup)\n```"
|
76
|
+
assert_equal content, highlight_code_blocks("file.rst", content)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "highlights one-line code block" do
|
80
|
+
content = "```cl\n(yup)\n```"
|
81
|
+
assert_match "##[cl](yup)##", highlight_code_blocks("file.md", content)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "highlights multi-line code block" do
|
85
|
+
content = "```cl\n(yup)\n(yessir-p t)\n```"
|
86
|
+
assert_match "##[cl](yup)\n(yessir-p t)##", highlight_code_blocks("file.md", content)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "preserves code block white-space" do
|
90
|
+
content = "```cl\n(yup\n (yessir-p t))\n```"
|
91
|
+
assert_match "##[cl](yup\n (yessir-p t))##", highlight_code_blocks("file.md", content)
|
92
|
+
end
|
29
93
|
end
|
30
94
|
end
|
data/vendor/ui/css/gitorious.css
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dolt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 1
|
10
|
+
version: 0.5.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Johansen
|