octopress-pygments 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in octopress-pygments.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brandon Mathis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Octopress::Pygments
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'octopress-pygments'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install octopress-pygments
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,198 @@
1
+ require "octopress-pygments/version"
2
+ require 'fileutils'
3
+ require 'digest/md5'
4
+ require 'pygments'
5
+ require 'colorator'
6
+
7
+ PYGMENTS_CACHE_DIR = '.pygments-cache'
8
+ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
9
+
10
+ module Octopress
11
+ module Pygments
12
+
13
+ def self.render_pygments(code, lang)
14
+ highlighted_code = ::Pygments.highlight(code, :lexer => lang, :formatter => 'html', :options => {:encoding => 'utf-8'})
15
+ highlighted_code = highlighted_code.gsub(/{{/, '{{').gsub(/{%/, '{%')
16
+ highlighted_code.to_s
17
+ end
18
+
19
+ def self.highlight(code, options = {})
20
+ lang = options[:lang]
21
+ lang = 'ruby' if lang == 'ru'
22
+ lang = 'objc' if lang == 'm'
23
+ lang = 'perl' if lang == 'pl'
24
+ lang = 'yaml' if lang == 'yml'
25
+ lang = 'coffeescript' if lang == 'coffee'
26
+ lang = 'csharp' if lang == 'cs'
27
+ lang = 'plain' if lang == '' or lang.nil? or !lang
28
+
29
+ options[:lang] = lang
30
+ options[:title] ||= ' ' if options[:url]
31
+
32
+ # Attempt to retrieve cached code
33
+ cache = nil
34
+ unless options[:no_cache]
35
+ path = options[:cache_path] || get_cache_path(PYGMENTS_CACHE_DIR, options[:lang], options.to_s + code)
36
+ cache = read_cache(path)
37
+ end
38
+
39
+ unless cache
40
+ if options[:lang] == 'plain'
41
+ # Escape html tags
42
+ code = code.gsub('<','&lt;')
43
+ else
44
+ code = render_pygments(code, options[:lang]).match(/<pre>(.+)<\/pre>/m)[1].gsub(/ *$/, '') #strip out divs <div class="highlight">
45
+ end
46
+ code = tableize_code(code, options[:lang], {linenos: options[:linenos], start: options[:start], marks: options[:marks]})
47
+ title = captionize(options[:title], options[:url], options[:link_text]) if options[:title]
48
+ code = "<figure class='code'>#{title}#{code}</figure>"
49
+ File.open(path, 'w') {|f| f.print(code) } unless options[:no_cache]
50
+ end
51
+ cache || code
52
+ end
53
+
54
+ def self.read_cache (path)
55
+ File.exist?(path) ? File.read(path) : nil unless path.nil?
56
+ end
57
+
58
+ def self.get_cache_path (dir, name, str)
59
+ File.join(dir, "#{name}-#{Digest::MD5.hexdigest(str)}.html")
60
+ end
61
+
62
+ def self.captionize (caption, url, link_text)
63
+ figcaption = "<figcaption>#{caption}"
64
+ figcaption += "<a href='#{url}'>#{(link_text || 'link').strip}</a>" if url
65
+ figcaption += "</figcaption>"
66
+ end
67
+
68
+ def self.tableize_code (code, lang, options = {})
69
+ start = options[:start] || 1
70
+ lines = options[:linenos] || true
71
+ marks = options[:marks] || []
72
+ table = "<div class='highlight'><table><tr>"
73
+ table += number_lines(start, code.lines.count, marks) if lines
74
+ table += "<td class='main #{'unnumbered' unless lines} #{lang}'><pre>"
75
+ code.lines.each_with_index do |line,index|
76
+ classes = 'line'
77
+ if marks.include? index + start
78
+ classes += ' marked'
79
+ classes += ' start' unless marks.include? index - 1 + start
80
+ classes += ' end' unless marks.include? index + 1 + start
81
+ end
82
+ line = line.strip.empty? ? ' ' : line
83
+ table += "<div class='#{classes}'>#{line}</div>"
84
+ end
85
+ table +="</pre></td></tr></table></div>"
86
+ end
87
+
88
+ def self.number_lines (start, count, marks)
89
+ start ||= 1
90
+ lines = "<td class='line-numbers' aria-hidden='true'><pre>"
91
+ count.times do |index|
92
+ classes = 'line-number'
93
+ if marks.include? index + start
94
+ classes += ' marked'
95
+ classes += ' start' unless marks.include? index - 1 + start
96
+ classes += ' end' unless marks.include? index + 1 + start
97
+ end
98
+ lines += "<div data-line='#{index + start}' class='#{classes}'></div>"
99
+ end
100
+ lines += "</pre></td>"
101
+ end
102
+
103
+ def self.parse_markup (input, defaults={})
104
+ lang = input.match(/\s*lang:\s*(\S+)/i)
105
+ lang = (lang.nil? ? nil : lang[1])
106
+
107
+ url = input.match(/\s*url:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
108
+ url = (url.nil? ? nil : url[3] || url[5] || url[6])
109
+
110
+ title = input.match(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
111
+ title = (title.nil? ? nil : title[3] || title[5] || title[6])
112
+ title ||= ' ' if url
113
+
114
+ linenos = input.match(/\s*linenos:\s*(\w+)/i)
115
+ linenos = (linenos.nil? ? nil : linenos[1])
116
+
117
+ marks = get_marks(input)
118
+
119
+ link_text = input.match(/\s*link[-_]text:\s*(("(.+?)")|('(.+?)')|(\S+))/i)
120
+ link_text = (link_text.nil? ? 'link' : link_text[3] || link_text[5] || link_text[6])
121
+
122
+ start = input.match(/\s*start:\s*(\d+)/i)
123
+ start = (start.nil? ? nil : start[1].to_i)
124
+
125
+ endline = input.match(/\s*end:\s*(\d+)/i)
126
+ endline = (endline.nil? ? nil : endline[1].to_i)
127
+
128
+ if input =~ / *range:(\d+)-(\d+)/i
129
+ start = $1.to_i
130
+ endline = $2.to_i
131
+ end
132
+
133
+ options = {
134
+ lang: lang,
135
+ url: url,
136
+ title: title,
137
+ linenos: linenos,
138
+ marks: marks,
139
+ link_text: link_text,
140
+ start: start,
141
+ end: endline
142
+ }
143
+
144
+ defaults.each { |k,v| options[k] ||= defaults[k] }
145
+ options
146
+ end
147
+
148
+ def self.clean_markup (input)
149
+ input.sub(/\s*lang:\s*\S+/i,''
150
+ ).sub(/\s*title:\s*(("(.+?)")|('(.+?)')|(\S+))/i,''
151
+ ).sub(/\s*url:\s*(\S+)/i,''
152
+ ).sub(/\s*link_text:\s*(("(.+?)")|('(.+?)')|(\S+))/i,''
153
+ ).sub(/\s*mark:\s*\d\S*/i,''
154
+ ).sub(/\s*linenos:\s*\w+/i,''
155
+ ).sub(/\s*start:\s*\d+/i,''
156
+ ).sub(/\s*end:\s*\d+/i,''
157
+ ).sub(/\s*range:\s*\d+-\d+/i,'')
158
+ end
159
+
160
+ def self.get_marks (input)
161
+ # Matches pattern for line marks and returns array of line numbers to mark
162
+ # Example input mark:1,5-10,2
163
+ # Outputs: [1,2,5,6,7,8,9,10]
164
+ marks = []
165
+ if input =~ / *mark:(\d\S*)/i
166
+ marks = $1.gsub /(\d+)-(\d+)/ do
167
+ ($1.to_i..$2.to_i).to_a.join(',')
168
+ end
169
+ marks = marks.split(',').collect {|s| s.to_i}.sort
170
+ end
171
+ marks
172
+ end
173
+
174
+ def self.get_range (code, start, endline)
175
+ length = code.lines.count
176
+ start ||= 1
177
+ endline ||= length
178
+ if start > 1 or endline < length
179
+ raise "#{filepath} is #{length} lines long, cannot begin at line #{start}" if start > length
180
+ raise "#{filepath} is #{length} lines long, cannot read beyond line #{endline}" if endline > length
181
+ code = code.split(/\n/).slice(start - 1, endline + 1 - start).join("\n")
182
+ end
183
+ code
184
+ end
185
+
186
+ def self.highlight_failed(error, syntax, markup, code, file = nil)
187
+ code_snippet = code.split("\n")[0..9].map{|l| " #{l}" }.join("\n")
188
+ fail_message = "\nPygments Error while parsing the following markup#{" in #{file}" if file}:\n\n".red
189
+ fail_message += " #{markup}\n#{code_snippet}\n"
190
+ fail_message += "#{" ..." if code.split("\n").size > 10}\n"
191
+ fail_message += "\nValid Syntax:\n\n#{syntax}\n".yellow
192
+ fail_message += "\nPygments Error:\n\n#{error.message}".red
193
+ $stderr.puts fail_message.chomp
194
+ raise ArgumentError
195
+ end
196
+ end
197
+ end
198
+
@@ -0,0 +1,5 @@
1
+ module Octopress
2
+ module Pygments
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'octopress-pygments/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "octopress-pygments"
8
+ gem.version = Octopress::Pygments::VERSION
9
+ gem.authors = ["Brandon Mathis"]
10
+ gem.email = ["brandon@imathis.com"]
11
+ gem.description = %q{Octopress's core plugin for rendering nice code blocks}
12
+ gem.summary = %q{Octopress's core plugin for rendering nice code blocks}
13
+ gem.homepage = "https://github.com/octopress/octopress-pygments"
14
+ gem.license = "MIT"
15
+
16
+ gem.add_runtime_dependency 'pygments.rb >= 0.5'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: octopress-pygments
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brandon Mathis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pygments.rb >= 0.5
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Octopress's core plugin for rendering nice code blocks
31
+ email:
32
+ - brandon@imathis.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/octopress-pygments.rb
43
+ - lib/octopress-pygments/version.rb
44
+ - octopress-pygments.gemspec
45
+ homepage: https://github.com/octopress/octopress-pygments
46
+ licenses:
47
+ - MIT
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Octopress's core plugin for rendering nice code blocks
70
+ test_files: []