gitdoc 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/doc.haml +1 -3
  3. data/gitdoc.rb +53 -7
  4. data/highlight.css +61 -0
  5. metadata +6 -5
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 2.0.0
data/doc.haml CHANGED
@@ -2,8 +2,6 @@
2
2
  %head
3
3
  %title= settings.title || 'Documents'
4
4
  %meta{:charset => "utf-8"}
5
+ %link{:rel => :stylesheet, :href => '/.css'}
5
6
  = settings.header
6
- %style
7
- = sass :reset
8
- = styles
9
7
  #doc!= @doc
data/gitdoc.rb CHANGED
@@ -2,6 +2,7 @@ require 'sinatra'
2
2
  require 'rdiscount'
3
3
  require 'haml'
4
4
  require 'sass'
5
+ require 'digest/sha1'
5
6
 
6
7
  def GitDoc! title = nil, opts = {}
7
8
  dir = File.dirname(File.expand_path(caller.first.split(':').first))
@@ -19,19 +20,56 @@ disable :logging # the server always writes its own log anyway
19
20
  helpers do
20
21
 
21
22
  def md source
22
- RDiscount.new(source).to_html.
23
- # uses newline entity in pre tags
24
- gsub(/<code>.*?<\/code>/m) do |match|
25
- match.gsub(/\n/m,"&#x000A;")
23
+ source_without_code = extract_code source
24
+ html = RDiscount.new(source_without_code).to_html
25
+ html = highlight_code html
26
+ newline_entities_for_tag :pre, html
27
+ end
28
+
29
+ # extract_code and highlight_code cribbed from:
30
+ # https://github.com/github/gollum/blob/0b8bc597a7e9495b272e5dbb743827f56ccd2fe6/lib/gollum/markup.rb#L367
31
+
32
+ # Replaces all code fragments with a SHA1 hash. Stores the original fragment
33
+ # in @codemap
34
+ def extract_code source
35
+ @codemap = {}
36
+ source.gsub(/^``` ?(.+?)\r?\n(.+?)\r?\n```\r?$/m) do
37
+ Digest::SHA1.hexdigest($2).tap { |id| @codemap[id] = { :lang => $1, :code => $2 } }
26
38
  end
27
39
  end
28
40
 
29
- def styles
30
- sass File.read(settings.styles) if File.exist? settings.styles
41
+ # Replaces all SHA1 hash strings present in @codemap with pygmentized
42
+ # html suitable for coloring with a stylesheet
43
+ def highlight_code html
44
+ @codemap.each do |id, spec|
45
+ formatted = begin
46
+ # TODO: fix. fails silenty right now
47
+ IO.popen("pygmentize -l #{spec[:lang]} -f html", 'r+') do |io|
48
+ io << unindent(spec[:code])
49
+ io.close_write
50
+ io.read.strip
51
+ end
52
+ end
53
+ html.gsub!(id, formatted)
54
+ end
55
+ html
31
56
  end
32
57
 
33
- end
58
+ # Removes leading indent if all non-blank lines are indented
59
+ def unindent code
60
+ code.gsub!(/^( |\t)/m, '') if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
61
+ code
62
+ end
34
63
 
64
+ # Allows the rendered markdown to be indented in its containing document
65
+ # without introducing extra whitespace into preformatted blocks
66
+ def newline_entities_for_tag tag, html
67
+ html.gsub(/<#{tag}>.*?<\/#{tag}>/m) do |match|
68
+ match.gsub(/\n/m,"&#x000A;")
69
+ end
70
+ end
71
+
72
+ end
35
73
 
36
74
  get '/*' do |name|
37
75
  name = 'index' if name.empty?
@@ -47,4 +85,12 @@ get '/*.*' do |name,ext|
47
85
  send_file file
48
86
  end
49
87
 
88
+ get '/.css' do
89
+ content_type :css
90
+ styles = sass(:reset)
91
+ styles += File.read(settings.root + '/highlight.css')
92
+ styles += sass(File.read(settings.styles)) if File.exist? settings.styles
93
+ styles
94
+ end
95
+
50
96
  not_found { "404. Oh Noes!" }
data/highlight.css ADDED
@@ -0,0 +1,61 @@
1
+ .highlight { background: #ffffff; }
2
+ .highlight .c { color: #999988; font-style: italic } /* Comment */
3
+ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
4
+ .highlight .k { font-weight: bold } /* Keyword */
5
+ .highlight .o { font-weight: bold } /* Operator */
6
+ .highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */
7
+ .highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
8
+ .highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */
9
+ .highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
10
+ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
11
+ .highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
12
+ .highlight .ge { font-style: italic } /* Generic.Emph */
13
+ .highlight .gr { color: #aa0000 } /* Generic.Error */
14
+ .highlight .gh { color: #999999 } /* Generic.Heading */
15
+ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
16
+ .highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
17
+ .highlight .gc { color: #999; background-color: #EAF2F5 }
18
+ .highlight .go { color: #888888 } /* Generic.Output */
19
+ .highlight .gp { color: #555555 } /* Generic.Prompt */
20
+ .highlight .gs { font-weight: bold } /* Generic.Strong */
21
+ .highlight .gu { color: #aaaaaa } /* Generic.Subheading */
22
+ .highlight .gt { color: #aa0000 } /* Generic.Traceback */
23
+ .highlight .kc { font-weight: bold } /* Keyword.Constant */
24
+ .highlight .kd { font-weight: bold } /* Keyword.Declaration */
25
+ .highlight .kp { font-weight: bold } /* Keyword.Pseudo */
26
+ .highlight .kr { font-weight: bold } /* Keyword.Reserved */
27
+ .highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */
28
+ .highlight .m { color: #009999 } /* Literal.Number */
29
+ .highlight .s { color: #d14 } /* Literal.String */
30
+ .highlight .na { color: #008080 } /* Name.Attribute */
31
+ .highlight .nb { color: #0086B3 } /* Name.Builtin */
32
+ .highlight .nc { color: #445588; font-weight: bold } /* Name.Class */
33
+ .highlight .no { color: #008080 } /* Name.Constant */
34
+ .highlight .ni { color: #800080 } /* Name.Entity */
35
+ .highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */
36
+ .highlight .nf { color: #990000; font-weight: bold } /* Name.Function */
37
+ .highlight .nn { color: #555555 } /* Name.Namespace */
38
+ .highlight .nt { color: #000080 } /* Name.Tag */
39
+ .highlight .nv { color: #008080 } /* Name.Variable */
40
+ .highlight .ow { font-weight: bold } /* Operator.Word */
41
+ .highlight .w { color: #bbbbbb } /* Text.Whitespace */
42
+ .highlight .mf { color: #009999 } /* Literal.Number.Float */
43
+ .highlight .mh { color: #009999 } /* Literal.Number.Hex */
44
+ .highlight .mi { color: #009999 } /* Literal.Number.Integer */
45
+ .highlight .mo { color: #009999 } /* Literal.Number.Oct */
46
+ .highlight .sb { color: #d14 } /* Literal.String.Backtick */
47
+ .highlight .sc { color: #d14 } /* Literal.String.Char */
48
+ .highlight .sd { color: #d14 } /* Literal.String.Doc */
49
+ .highlight .s2 { color: #d14 } /* Literal.String.Double */
50
+ .highlight .se { color: #d14 } /* Literal.String.Escape */
51
+ .highlight .sh { color: #d14 } /* Literal.String.Heredoc */
52
+ .highlight .si { color: #d14 } /* Literal.String.Interpol */
53
+ .highlight .sx { color: #d14 } /* Literal.String.Other */
54
+ .highlight .sr { color: #009926 } /* Literal.String.Regex */
55
+ .highlight .s1 { color: #d14 } /* Literal.String.Single */
56
+ .highlight .ss { color: #990073 } /* Literal.String.Symbol */
57
+ .highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */
58
+ .highlight .vc { color: #008080 } /* Name.Variable.Class */
59
+ .highlight .vg { color: #008080 } /* Name.Variable.Global */
60
+ .highlight .vi { color: #008080 } /* Name.Variable.Instance */
61
+ .highlight .il { color: #009999 } /* Literal.Number.Integer.Long */
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitdoc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
- - 1
8
- - 1
7
+ - 2
9
8
  - 0
10
- version: 1.1.0
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Myles Byrne
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-04 00:00:00 -05:00
18
+ date: 2010-11-29 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -79,6 +79,7 @@ files:
79
79
  - VERSION
80
80
  - doc.haml
81
81
  - gitdoc.rb
82
+ - highlight.css
82
83
  - reset.sass
83
84
  has_rdoc: true
84
85
  homepage: http://github.com/quackingduck/gitdoc