glorify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .rvmrc
2
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1 @@
1
+ # glorify
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/example/app.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'glorify'
4
+ require 'erb'
5
+
6
+ get '/' do
7
+ @example = File.open("#{File.dirname(__FILE__)}/example.md", "rb").read
8
+ erb :index
9
+ end
10
+
11
+ __END__
12
+ @@layout
13
+ <html>
14
+ <head>
15
+ <link rel="stylesheet" type="text/css" href="/pygments.css" />
16
+ </head>
17
+ <body>
18
+ <%= yield %>
19
+ </body>
20
+ </html>
21
+
22
+ @@index
23
+ <%= glorify @example %>
@@ -0,0 +1,14 @@
1
+ # a short example of glorify
2
+
3
+ this is just a quick example of how to use glorify
4
+
5
+ ```ruby
6
+ def example
7
+ puts "Hello, world!"
8
+ end
9
+ example
10
+ #=> Hello, world!
11
+ ```
12
+
13
+ That should parse into a code block highlighted for ruby.
14
+
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'sinatra/base'
3
+ require 'glorify'
4
+ require 'erb'
5
+
6
+ class SubclassedApp < Sinatra::Base
7
+ register Sinatra::Glorify
8
+
9
+ set :views, File.dirname(__FILE__)
10
+ enable :inline_templates
11
+
12
+ get '/' do
13
+ @example = File.open("#{File.dirname(__FILE__)}/example.md", "rb").read
14
+ erb :index
15
+ end
16
+
17
+ run! if app_file == $0
18
+ end
19
+
20
+ __END__
21
+ @@layout
22
+ <html>
23
+ <head>
24
+ <link rel="stylesheet" type="text/css" href="/pygments.css" />
25
+ </head>
26
+ <body>
27
+ <%= yield %>
28
+ </body>
29
+ </html>
30
+
31
+ @@index
32
+ <%= glorify @example %>
data/glorify.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "glorify/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "glorify"
7
+ s.version = Sinatra::Glorify::VERSION
8
+ s.authors = ["Zachary Scott"]
9
+ s.email = ["zachary@zacharyscott.net"]
10
+ s.homepage = "http://github.com/zzak/glorify"
11
+ s.summary = %q{Sinatra helper to parse markdown with syntax highlighting like the pros}
12
+ s.description = %q{Renders via redcarpet with syntax highlighting thanks to albino. Able to use fenced code blocks like github, and includes a default pygments stylesheet.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ["lib"]
16
+
17
+ s.add_runtime_dependency "sinatra"
18
+ s.add_runtime_dependency "redcarpet", "~> 1.0"
19
+ s.add_runtime_dependency "albino"
20
+ s.add_runtime_dependency "nokogiri"
21
+ end
data/lib/glorify.rb ADDED
@@ -0,0 +1,125 @@
1
+ require "sinatra/base"
2
+ require "glorify/version"
3
+ require "redcarpet"
4
+ require "albino"
5
+ require "nokogiri"
6
+ require "net/http"
7
+
8
+ module Sinatra
9
+ module Glorify
10
+ module Helpers
11
+ def glorify text
12
+ options = [:filter_html, :autolink,
13
+ :no_intraemphasis, :fenced_code, :gh_blockcode]
14
+ highlighter(Redcarpet.new(text, *options).to_html)
15
+ end
16
+
17
+ def glorify_css
18
+ <<-css
19
+ .hll { background-color: #ffffcc }
20
+ .c { color: #888888 } /* Comment */
21
+ .err { color: #a61717; background-color: #e3d2d2 } /* Error */
22
+ .k { color: #008800; font-weight: bold } /* Keyword */
23
+ .cm { color: #888888 } /* Comment.Multiline */
24
+ .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
25
+ .c1 { color: #888888 } /* Comment.Single */
26
+ .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
27
+ .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
28
+ .ge { font-style: italic } /* Generic.Emph */
29
+ .gr { color: #aa0000 } /* Generic.Error */
30
+ .gh { color: #303030 } /* Generic.Heading */
31
+ .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
32
+ .go { color: #888888 } /* Generic.Output */
33
+ .gp { color: #555555 } /* Generic.Prompt */
34
+ .gs { font-weight: bold } /* Generic.Strong */
35
+ .gu { color: #606060 } /* Generic.Subheading */
36
+ .gt { color: #aa0000 } /* Generic.Traceback */
37
+ .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
38
+ .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
39
+ .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
40
+ .kp { color: #008800 } /* Keyword.Pseudo */
41
+ .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
42
+ .kt { color: #888888; font-weight: bold } /* Keyword.Type */
43
+ .m { color: #0000DD; font-weight: bold } /* Literal.Number */
44
+ .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
45
+ .na { color: #336699 } /* Name.Attribute */
46
+ .nb { color: #003388 } /* Name.Builtin */
47
+ .nc { color: #bb0066; font-weight: bold } /* Name.Class */
48
+ .no { color: #003366; font-weight: bold } /* Name.Constant */
49
+ .nd { color: #555555 } /* Name.Decorator */
50
+ .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
51
+ .nf { color: #0066bb; font-weight: bold } /* Name.Function */
52
+ .nl { color: #336699; font-style: italic } /* Name.Label */
53
+ .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
54
+ .py { color: #336699; font-weight: bold } /* Name.Property */
55
+ .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
56
+ .nv { color: #336699 } /* Name.Variable */
57
+ .ow { color: #008800 } /* Operator.Word */
58
+ .w { color: #bbbbbb } /* Text.Whitespace */
59
+ .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
60
+ .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
61
+ .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
62
+ .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
63
+ .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
64
+ .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
65
+ .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
66
+ .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
67
+ .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
68
+ .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
69
+ .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
70
+ .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
71
+ .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
72
+ .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
73
+ .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
74
+ .bp { color: #003388 } /* Name.Builtin.Pseudo */
75
+ .vc { color: #336699 } /* Name.Variable.Class */
76
+ .vg { color: #dd7700 } /* Name.Variable.Global */
77
+ .vi { color: #3333bb } /* Name.Variable.Instance */
78
+ .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
79
+ css
80
+ end
81
+
82
+ private
83
+ def highlighter html
84
+ doc = Nokogiri::HTML(html)
85
+ doc.search("//pre[@lang]").each do |pre|
86
+ pre.replace colorize(pre.text.rstrip, pre[:lang])
87
+ end
88
+ doc.search('pre').each do |pre|
89
+ pre.children.each do |c|
90
+ c.parent = pre.parent
91
+ end
92
+ pre.remove
93
+ end
94
+ doc.search('div').each do |div|
95
+ if div['class'] == 'highlight'
96
+ div.replace(Nokogiri.make("<pre>#{div.to_html}</pre>"))
97
+ end
98
+ end
99
+ doc.to_s
100
+ end
101
+
102
+ def colorize(code, lang)
103
+ if(pygmentize?)
104
+ Albino.colorize(code, lang)
105
+ else
106
+ Net::HTTP.post_form(URI.parse('http://pygments.appspot.com/'),
107
+ {'code'=>code, 'lang'=>lang}).body
108
+ end
109
+ end
110
+
111
+ def pygmentize?
112
+ system 'pygmentize -V'
113
+ end
114
+ end
115
+
116
+ def self.registered(app)
117
+ app.helpers Glorify::Helpers
118
+ app.get '/pygments.css' do
119
+ glorify_css
120
+ end
121
+ end
122
+ end
123
+
124
+ register Glorify
125
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module Glorify
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glorify
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Zachary Scott
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-11 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sinatra
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: redcarpet
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 15
43
+ segments:
44
+ - 1
45
+ - 0
46
+ version: "1.0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: albino
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: nokogiri
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ description: Renders via redcarpet with syntax highlighting thanks to albino. Able to use fenced code blocks like github, and includes a default pygments stylesheet.
78
+ email:
79
+ - zachary@zacharyscott.net
80
+ executables: []
81
+
82
+ extensions: []
83
+
84
+ extra_rdoc_files: []
85
+
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - LICENSE
90
+ - README.md
91
+ - Rakefile
92
+ - example/app.rb
93
+ - example/example.md
94
+ - example/subclassed_app.rb
95
+ - glorify.gemspec
96
+ - lib/glorify.rb
97
+ - lib/glorify/version.rb
98
+ homepage: http://github.com/zzak/glorify
99
+ licenses: []
100
+
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.13
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Sinatra helper to parse markdown with syntax highlighting like the pros
131
+ test_files: []
132
+