glorify 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  .rvmrc
2
2
  pkg/*
3
3
  *.swp
4
+ .rbx
5
+ Gemfile.lock
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Sinatra helper to parse markdown with syntax highlighting like the pros
4
4
 
5
- Renders via redcarpet with syntax highlighting thanks to albino. Able to use
5
+ Renders via redcarpet with syntax highlighting thanks to [pygments.rb](https://github.com/tmm1/pygments.rb). Able to use
6
6
  fenced code blocks like github, and includes a default pygments stylesheet.
7
7
 
8
8
  ## install
@@ -74,7 +74,7 @@ end
74
74
 
75
75
  this is just a simple `erb` template, but you get the idea.
76
76
 
77
- ```ruby
77
+ ```erb
78
78
  <html>
79
79
  <head>
80
80
  <link rel="stylesheet" type="text/css" href="/pygments.css" />
@@ -85,9 +85,20 @@ this is just a simple `erb` template, but you get the idea.
85
85
  </html>
86
86
  ```
87
87
 
88
+ ### on heroku
89
+
90
+ to make this work on heroku you'll have to use python2.6
91
+
92
+ ```bash
93
+ heroku config:add GLORIFY_PYTHON=python2.6
94
+ ```
95
+ thanks to @simon for
96
+ [pointing this out](https://github.com/zzak/glorify/pull/5#r812124)
97
+
98
+
88
99
  ## license
89
100
 
90
- ```plaintext
101
+ ```
91
102
  Permission is hereby granted, free of charge, to any person obtaining a copy
92
103
  of this software and associated documentation files (the 'Software'), to deal
93
104
  in the Software without restriction, including without limitation the rights
data/Rakefile CHANGED
@@ -1 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "spec/*_spec.rb"
7
+ end
8
+
9
+ task(:spec => :test)
10
+ task(:default => :test)
@@ -5,17 +5,21 @@ require "glorify/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "glorify"
7
7
  s.version = Sinatra::Glorify::VERSION
8
- s.authors = ["Zachary Scott", "Jonathan Stott"]
8
+ s.authors = ["Zachary Scott", "Jonathan Stott", "Simon Gate"]
9
9
  s.email = ["zachary@zacharyscott.net"]
10
10
  s.homepage = "http://github.com/zzak/glorify"
11
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.}
12
+ s.description = %q{Renders via redcarpet with syntax highlighting thanks to pygments.rb. Able to use fenced code blocks like github, and includes a default pygments stylesheet.}
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.require_paths = ["lib"]
16
16
 
17
17
  s.add_runtime_dependency "sinatra"
18
18
  s.add_runtime_dependency "redcarpet", "~> 2.0"
19
- s.add_runtime_dependency "albino"
19
+ s.add_runtime_dependency "pygments.rb"
20
20
  s.add_runtime_dependency "nokogiri"
21
+
22
+ s.add_development_dependency "minitest"
23
+ s.add_development_dependency "rack-test"
24
+ s.add_development_dependency "rake"
21
25
  end
@@ -1,4 +1,5 @@
1
1
  require "sinatra/base"
2
+ require "glorify/css"
2
3
  require "glorify/version"
3
4
  require "glorify/renderer"
4
5
 
@@ -6,78 +7,12 @@ module Sinatra
6
7
  module Glorify
7
8
  module Helpers
8
9
  def glorify text
9
- rndr = Glorify::Renderer.new(:use_albino => settings.use_albino)
10
+ rndr = Glorify::Renderer.new
10
11
  Redcarpet::Markdown.new(rndr, settings.glorify_extensions).render(text)
11
12
  end
12
-
13
- def glorify_css
14
- <<-css
15
- .hll { background-color: #ffffcc }
16
- .c { color: #888888 } /* Comment */
17
- .err { color: #a61717; background-color: #e3d2d2 } /* Error */
18
- .k { color: #008800; font-weight: bold } /* Keyword */
19
- .cm { color: #888888 } /* Comment.Multiline */
20
- .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
21
- .c1 { color: #888888 } /* Comment.Single */
22
- .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
23
- .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
24
- .ge { font-style: italic } /* Generic.Emph */
25
- .gr { color: #aa0000 } /* Generic.Error */
26
- .gh { color: #303030 } /* Generic.Heading */
27
- .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
28
- .go { color: #888888 } /* Generic.Output */
29
- .gp { color: #555555 } /* Generic.Prompt */
30
- .gs { font-weight: bold } /* Generic.Strong */
31
- .gu { color: #606060 } /* Generic.Subheading */
32
- .gt { color: #aa0000 } /* Generic.Traceback */
33
- .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
34
- .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
35
- .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
36
- .kp { color: #008800 } /* Keyword.Pseudo */
37
- .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
38
- .kt { color: #888888; font-weight: bold } /* Keyword.Type */
39
- .m { color: #0000DD; font-weight: bold } /* Literal.Number */
40
- .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
41
- .na { color: #336699 } /* Name.Attribute */
42
- .nb { color: #003388 } /* Name.Builtin */
43
- .nc { color: #bb0066; font-weight: bold } /* Name.Class */
44
- .no { color: #003366; font-weight: bold } /* Name.Constant */
45
- .nd { color: #555555 } /* Name.Decorator */
46
- .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
47
- .nf { color: #0066bb; font-weight: bold } /* Name.Function */
48
- .nl { color: #336699; font-style: italic } /* Name.Label */
49
- .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
50
- .py { color: #336699; font-weight: bold } /* Name.Property */
51
- .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
52
- .nv { color: #336699 } /* Name.Variable */
53
- .ow { color: #008800 } /* Operator.Word */
54
- .w { color: #bbbbbb } /* Text.Whitespace */
55
- .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
56
- .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
57
- .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
58
- .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
59
- .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
60
- .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
61
- .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
62
- .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
63
- .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
64
- .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
65
- .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
66
- .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
67
- .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
68
- .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
69
- .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
70
- .bp { color: #003388 } /* Name.Builtin.Pseudo */
71
- .vc { color: #336699 } /* Name.Variable.Class */
72
- .vg { color: #dd7700 } /* Name.Variable.Global */
73
- .vi { color: #3333bb } /* Name.Variable.Instance */
74
- .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
75
- css
76
- end
77
13
  end
78
14
 
79
15
  def self.registered(app)
80
- app.set :use_albino, proc { system 'pygmentize -V' }
81
16
  app.set :glorify_extensions, { :filter_html => true,
82
17
  :autolink => true,
83
18
  :no_intra_emphasis => true,
@@ -0,0 +1,70 @@
1
+ module Sinatra
2
+ module Glorify
3
+ module Helpers
4
+ def glorify_css
5
+ <<-css
6
+ .hll { background-color: #ffffcc }
7
+ .c { color: #888888 } /* Comment */
8
+ .err { color: #a61717; background-color: #e3d2d2 } /* Error */
9
+ .k { color: #008800; font-weight: bold } /* Keyword */
10
+ .cm { color: #888888 } /* Comment.Multiline */
11
+ .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
12
+ .c1 { color: #888888 } /* Comment.Single */
13
+ .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
14
+ .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
15
+ .ge { font-style: italic } /* Generic.Emph */
16
+ .gr { color: #aa0000 } /* Generic.Error */
17
+ .gh { color: #303030 } /* Generic.Heading */
18
+ .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
19
+ .go { color: #888888 } /* Generic.Output */
20
+ .gp { color: #555555 } /* Generic.Prompt */
21
+ .gs { font-weight: bold } /* Generic.Strong */
22
+ .gu { color: #606060 } /* Generic.Subheading */
23
+ .gt { color: #aa0000 } /* Generic.Traceback */
24
+ .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
25
+ .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
26
+ .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
27
+ .kp { color: #008800 } /* Keyword.Pseudo */
28
+ .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
29
+ .kt { color: #888888; font-weight: bold } /* Keyword.Type */
30
+ .m { color: #0000DD; font-weight: bold } /* Literal.Number */
31
+ .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
32
+ .na { color: #336699 } /* Name.Attribute */
33
+ .nb { color: #003388 } /* Name.Builtin */
34
+ .nc { color: #bb0066; font-weight: bold } /* Name.Class */
35
+ .no { color: #003366; font-weight: bold } /* Name.Constant */
36
+ .nd { color: #555555 } /* Name.Decorator */
37
+ .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
38
+ .nf { color: #0066bb; font-weight: bold } /* Name.Function */
39
+ .nl { color: #336699; font-style: italic } /* Name.Label */
40
+ .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
41
+ .py { color: #336699; font-weight: bold } /* Name.Property */
42
+ .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
43
+ .nv { color: #336699 } /* Name.Variable */
44
+ .ow { color: #008800 } /* Operator.Word */
45
+ .w { color: #bbbbbb } /* Text.Whitespace */
46
+ .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
47
+ .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
48
+ .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
49
+ .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
50
+ .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
51
+ .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
52
+ .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
53
+ .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
54
+ .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
55
+ .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
56
+ .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
57
+ .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
58
+ .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
59
+ .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
60
+ .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
61
+ .bp { color: #003388 } /* Name.Builtin.Pseudo */
62
+ .vc { color: #336699 } /* Name.Variable.Class */
63
+ .vg { color: #dd7700 } /* Name.Variable.Global */
64
+ .vi { color: #3333bb } /* Name.Variable.Instance */
65
+ .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
66
+ css
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,25 +1,24 @@
1
1
  require "redcarpet"
2
- require "albino"
3
- require "net/http"
2
+ require "pygments.rb"
4
3
 
5
4
  module Sinatra
6
5
  module Glorify
7
6
  class Renderer < Redcarpet::Render::HTML
8
- attr_reader :use_albino
9
7
 
10
8
  def initialize(options={})
11
- @use_albino = options.fetch(:use_albino, true)
9
+ python = ENV['GLORIFY_PYTHON'] || 'python'
10
+ RubyPython.configure :python_exe => python
12
11
  super
13
12
  end
14
13
 
15
14
  def block_code(code, lang)
16
- if use_albino
17
- Albino.colorize(code, lang)
18
- else
19
- Net::HTTP.post_form(URI.parse('http://pygments.appspot.com/'),
20
- {'code'=>code, 'lang'=>lang}).body
15
+ begin
16
+ Pygments.highlight(code, :lexer => lang, :options => {:encoding => "utf-8"})
17
+ rescue RubyPython::PythonError
18
+ Pygments.highlight(code, :options => {:encoding => "utf-8"})
21
19
  end
22
20
  end
21
+
23
22
  end
24
23
  end
25
24
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Glorify
3
- VERSION = "0.0.5"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ <%= glorify('```puts "Hello, world!"```') %>
@@ -0,0 +1 @@
1
+ <%= glorify("#a sip of glory") %>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1 @@
1
+ <%= glorify(@some_code) %>
@@ -0,0 +1,5 @@
1
+ ```ruby
2
+ def in_ruby?
3
+ return apples.all? { |a| a.to_s == 'ruby' }
4
+ end
5
+ ```
@@ -0,0 +1,43 @@
1
+ begin
2
+ require_relative 'spec_helper'
3
+ rescue NameError
4
+ require File.expand_path('../spec_helper', __FILE__)
5
+ end
6
+
7
+ describe Sinatra::Glorify do
8
+
9
+ it "should parse a header" do
10
+ mock_app do
11
+ get('/') { erb :header }
12
+ end
13
+ expected = "<h1>a sip of glory</h1>"
14
+ get('/')
15
+ assert ok?
16
+ assert_equal expected, body
17
+ end
18
+
19
+ it "should parse code blocks" do
20
+ mock_app do
21
+ get('/') { erb :blocks }
22
+ end
23
+ expected = "<p><code>puts &quot;Hello, world!&quot;</code></p>"
24
+ get('/')
25
+ assert ok?
26
+ assert_equal expected, body
27
+ end
28
+
29
+ it "should parse ruby blocks" do
30
+ mock_app do
31
+ get('/') do
32
+ @some_code = File.open(
33
+ File.expand_path('../glorify/some_code.md', __FILE__),
34
+ "rb"
35
+ ).read
36
+ erb :ruby_blocks
37
+ end
38
+ end
39
+ get('/')
40
+ assert ok?
41
+ refute_empty Nokogiri::HTML(body).search("//div[@class = 'highlight']/pre")
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+ require 'minitest/spec'
3
+ require 'minitest/autorun'
4
+ require 'rack'
5
+
6
+ require 'glorify'
7
+ require 'nokogiri'
8
+ require 'rack/test'
9
+ require 'sinatra/base'
10
+ require 'erb'
11
+
12
+ Sinatra::Base.set :environment, :test
13
+ Sinatra::Base.set :views, File.expand_path('../glorify', __FILE__)
14
+ Sinatra::Base.register Sinatra::Glorify
15
+
16
+ class MiniTest::Spec
17
+ include Rack::Test::Methods
18
+
19
+ def mock_app(base=Sinatra::Base, &block)
20
+ @app = Sinatra.new(base, &block)
21
+ end
22
+
23
+ def app
24
+ Rack::Lint.new(@app)
25
+ end
26
+
27
+ def body
28
+ last_response.body.gsub(/\n/,'')
29
+ end
30
+
31
+ def ok?
32
+ last_response.ok?
33
+ end
34
+ end
metadata CHANGED
@@ -1,22 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glorify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 5
10
- version: 0.0.5
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zachary Scott
14
14
  - Jonathan Stott
15
+ - Simon Gate
15
16
  autorequire:
16
17
  bindir: bin
17
18
  cert_chain: []
18
19
 
19
- date: 2012-04-14 00:00:00 Z
20
+ date: 2012-05-22 00:00:00 Z
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
22
23
  name: sinatra
@@ -48,7 +49,7 @@ dependencies:
48
49
  type: :runtime
49
50
  version_requirements: *id002
50
51
  - !ruby/object:Gem::Dependency
51
- name: albino
52
+ name: pygments.rb
52
53
  prerelease: false
53
54
  requirement: &id003 !ruby/object:Gem::Requirement
54
55
  none: false
@@ -75,7 +76,49 @@ dependencies:
75
76
  version: "0"
76
77
  type: :runtime
77
78
  version_requirements: *id004
78
- description: Renders via redcarpet with syntax highlighting thanks to albino. Able to use fenced code blocks like github, and includes a default pygments stylesheet.
79
+ - !ruby/object:Gem::Dependency
80
+ name: minitest
81
+ prerelease: false
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ type: :development
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ name: rack-test
95
+ prerelease: false
96
+ requirement: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ type: :development
106
+ version_requirements: *id006
107
+ - !ruby/object:Gem::Dependency
108
+ name: rake
109
+ prerelease: false
110
+ requirement: &id007 !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ type: :development
120
+ version_requirements: *id007
121
+ description: Renders via redcarpet with syntax highlighting thanks to pygments.rb. Able to use fenced code blocks like github, and includes a default pygments stylesheet.
79
122
  email:
80
123
  - zachary@zacharyscott.net
81
124
  executables: []
@@ -91,8 +134,16 @@ files:
91
134
  - Rakefile
92
135
  - glorify.gemspec
93
136
  - lib/glorify.rb
137
+ - lib/glorify/css.rb
94
138
  - lib/glorify/renderer.rb
95
139
  - lib/glorify/version.rb
140
+ - spec/glorify/blocks.erb
141
+ - spec/glorify/header.erb
142
+ - spec/glorify/layout.erb
143
+ - spec/glorify/ruby_blocks.erb
144
+ - spec/glorify/some_code.md
145
+ - spec/glorify_spec.rb
146
+ - spec/spec_helper.rb
96
147
  homepage: http://github.com/zzak/glorify
97
148
  licenses: []
98
149
 
@@ -122,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
173
  requirements: []
123
174
 
124
175
  rubyforge_project:
125
- rubygems_version: 1.8.13
176
+ rubygems_version: 1.8.24
126
177
  signing_key:
127
178
  specification_version: 3
128
179
  summary: Sinatra helper to parse markdown with syntax highlighting like the pros