markup_parser 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +9 -0
- data/lib/markup_parser/default.rb +8 -6
- data/lib/markup_parser/html.rb +0 -1
- data/lib/markup_parser/markdown.rb +6 -53
- data/lib/markup_parser/rdoc.rb +1 -1
- data/lib/markup_parser/version.rb +1 -1
- data/lib/markup_parser.rb +45 -2
- data/markup_parser.gemspec +6 -0
- metadata +59 -4
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
|
1
3
|
module MarkupParser
|
2
4
|
class Default
|
3
|
-
puts "\n**************\nMarkupParser::Default loaded\n**************\n"
|
4
5
|
attr_reader :original_text, :nokoguri_parser, :html_text, :lexer_proc
|
5
6
|
|
6
7
|
def initialize(text='', &lexer)
|
@@ -14,12 +15,13 @@ module MarkupParser
|
|
14
15
|
begin
|
15
16
|
nokoguri_parser.to_xhtml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XHTML)
|
16
17
|
rescue => e
|
17
|
-
puts
|
18
|
-
|
18
|
+
puts <<-ERROR
|
19
|
+
******************
|
19
20
|
Error in #{self.class}#to_html.
|
20
21
|
Reason: #{e.message}.
|
21
22
|
Putting error message into the output.
|
22
|
-
|
23
|
+
******************
|
24
|
+
ERROR
|
23
25
|
return "<p class='parse_error'>Error in parsing in #{self.class}: #{e.message}.</p>"
|
24
26
|
end
|
25
27
|
end
|
@@ -28,7 +30,7 @@ module MarkupParser
|
|
28
30
|
# Instantiates a Nokoguri::HTML fragment parser
|
29
31
|
def nokoguri_parser
|
30
32
|
begin
|
31
|
-
@nokoguri_parser ||= Nokogiri::HTML::DocumentFragment.parse(html_text)
|
33
|
+
@nokoguri_parser ||= ::Nokogiri::HTML::DocumentFragment.parse(html_text)
|
32
34
|
rescue => e
|
33
35
|
puts "
|
34
36
|
\n******************
|
@@ -36,7 +38,7 @@ module MarkupParser
|
|
36
38
|
Reason: #{e.message}.
|
37
39
|
Putting error message into the output.
|
38
40
|
******************\n"
|
39
|
-
@nokoguri_parser = Nokogiri::HTML::DocumentFragment.parse("<p class='parse_error'>Error in parsing in #{self.class}: #{e.message}.</p>")
|
41
|
+
@nokoguri_parser = ::Nokogiri::HTML::DocumentFragment.parse("<p class='parse_error'>Error in parsing in #{self.class}: #{e.message}.</p>")
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
data/lib/markup_parser/html.rb
CHANGED
@@ -1,14 +1,10 @@
|
|
1
|
+
require 'redcarpet'
|
2
|
+
require "#{File.dirname(__FILE__)}/markdown/string_extensions"
|
3
|
+
require "#{File.dirname(__FILE__)}/markdown/uv_html_render"
|
4
|
+
require "#{File.dirname(__FILE__)}/markdown/class_methods"
|
1
5
|
module MarkupParser
|
2
6
|
class Markdown < MarkupParser::Default
|
3
|
-
puts "\n**************\nMarkupParser::Markdown loaded\n**************\n"
|
4
7
|
|
5
|
-
# HTML renderer with Ultraviolet Code Lexer
|
6
|
-
class UvHtmlRender < Redcarpet::Render::HTML
|
7
|
-
puts "\n**************\nMarkupParser::Markdown::UvHtmlRender loaded\n**************\n"
|
8
|
-
def block_code(code, language)
|
9
|
-
Uv.parse(code, "xhtml", language, false, "railscasts")
|
10
|
-
end
|
11
|
-
end
|
12
8
|
|
13
9
|
|
14
10
|
# Only loads the Markdown parser once
|
@@ -36,11 +32,6 @@ module MarkupParser
|
|
36
32
|
}
|
37
33
|
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
35
|
# Returns the fully stylized HTML for this markdown text
|
45
36
|
def html_text
|
46
37
|
@html_text ||= parser.render(@original_text)
|
@@ -48,19 +39,17 @@ module MarkupParser
|
|
48
39
|
|
49
40
|
# Sets the parser to include as code lexer
|
50
41
|
def stylize_code_blocks
|
51
|
-
@parser = Markdown.html_parser_with_code_lexer
|
42
|
+
@parser = MarkupParser::Markdown.html_parser_with_code_lexer
|
52
43
|
self
|
53
44
|
end
|
54
45
|
|
55
46
|
|
56
|
-
|
57
|
-
|
58
47
|
private
|
59
48
|
|
60
49
|
# Instantiates the parser for this Markdown instance.
|
61
50
|
# Defaults to @@html_parser
|
62
51
|
def parser
|
63
|
-
@parser ||= Markdown.html_parser
|
52
|
+
@parser ||= MarkupParser::Markdown.html_parser
|
64
53
|
end
|
65
54
|
|
66
55
|
# Hacks parse to return this instence's parser for a later call
|
@@ -77,40 +66,4 @@ module MarkupParser
|
|
77
66
|
end
|
78
67
|
|
79
68
|
end
|
80
|
-
end
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
# Adds classes to String class...
|
86
|
-
class String
|
87
|
-
|
88
|
-
# Corrects the gh code block syntax mistake where one would write '~~~ .ruby'
|
89
|
-
# and the correct code should be '~~~ruby'
|
90
|
-
def correct_gh_code_syntax!
|
91
|
-
self.gsub!(/~~~\s\.([a-zA-Z]*)/, '~~~\1')
|
92
|
-
end
|
93
|
-
|
94
|
-
# Corrects the ol list elements: which only except the syntax: '1. ...'.
|
95
|
-
# Corrected syntaxes: '1)'
|
96
|
-
def correct_ol_list_parenth!
|
97
|
-
self.gsub!(/(\s*)(\d)\)/,'\1\2.')
|
98
|
-
end
|
99
|
-
|
100
|
-
# Converts tabs (\t) to 2 spaces
|
101
|
-
def convert_tabs_to_spaces!
|
102
|
-
self.gsub!(/\t/, " ")
|
103
|
-
end
|
104
|
-
|
105
|
-
# Standardize line endings
|
106
|
-
def standardize_newlines!
|
107
|
-
self.gsub!("\r\n", "\n")
|
108
|
-
self.gsub!("\r", "\n")
|
109
|
-
end
|
110
|
-
|
111
|
-
# Corrects the newlines by stripping the leading whitespace.
|
112
|
-
# NOTE: this is a hack to workaround the strange Gollum editor indentation behavior
|
113
|
-
def sub_newlines!
|
114
|
-
self.gsub!(/([\r\n|\n])[\ ]*(.)/,'\1\2')
|
115
|
-
end
|
116
69
|
end
|
data/lib/markup_parser/rdoc.rb
CHANGED
data/lib/markup_parser.rb
CHANGED
@@ -3,9 +3,52 @@ require "markup_parser/version"
|
|
3
3
|
module MarkupParser
|
4
4
|
puts "\n**************\nMarkupParser loaded\n**************\n"
|
5
5
|
|
6
|
-
|
6
|
+
PARSER_PATH = File.join(File.dirname(__FILE__), 'markup_parser')
|
7
|
+
|
8
|
+
|
9
|
+
def self.formats
|
10
|
+
parser_files.collect {|f| File.basename(f, '.rb') } - ["version"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.format_parsers
|
14
|
+
Hash[*formats.zip(parsers).flatten]
|
15
|
+
end
|
7
16
|
|
8
17
|
def self.parsers
|
9
|
-
|
18
|
+
formats.collect {|f| constantize(camelize("markup_parser/#{f}")) }
|
10
19
|
end
|
20
|
+
|
21
|
+
def self.reload_parsers
|
22
|
+
parser_files.each { |parser_file| require parser_file }
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def self.parser_files
|
30
|
+
Dir.glob( File.join(PARSER_PATH, '*.rb') )
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.constantize(camel_cased_word)
|
34
|
+
names = camel_cased_word.split('::')
|
35
|
+
names.shift if names.empty? || names.first.empty?
|
36
|
+
|
37
|
+
constant = Object
|
38
|
+
names.each do |name|
|
39
|
+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
40
|
+
end
|
41
|
+
constant
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
45
|
+
if first_letter_in_uppercase
|
46
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
47
|
+
else
|
48
|
+
lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
11
52
|
end
|
53
|
+
|
54
|
+
MarkupParser.reload_parsers
|
data/markup_parser.gemspec
CHANGED
@@ -15,5 +15,11 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = MarkupParser::VERSION
|
17
17
|
|
18
|
+
gem.add_dependency 'redcarpet', '>= 2.0.0b5'
|
19
|
+
gem.add_dependency 'nokogiri'
|
18
20
|
gem.add_dependency "uv"
|
21
|
+
gem.add_dependency 'rdoc'
|
22
|
+
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'minitest'
|
19
25
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markup_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,33 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redcarpet
|
16
|
+
requirement: &70241141367760 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.0.0b5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70241141367760
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: nokogiri
|
27
|
+
requirement: &70241141367300 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70241141367300
|
14
36
|
- !ruby/object:Gem::Dependency
|
15
37
|
name: uv
|
16
|
-
requirement: &
|
38
|
+
requirement: &70241141366840 !ruby/object:Gem::Requirement
|
17
39
|
none: false
|
18
40
|
requirements:
|
19
41
|
- - ! '>='
|
@@ -21,7 +43,40 @@ dependencies:
|
|
21
43
|
version: '0'
|
22
44
|
type: :runtime
|
23
45
|
prerelease: false
|
24
|
-
version_requirements: *
|
46
|
+
version_requirements: *70241141366840
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rdoc
|
49
|
+
requirement: &70241141366300 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70241141366300
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rake
|
60
|
+
requirement: &70241141365600 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70241141365600
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: &70241141364920 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70241141364920
|
25
80
|
description: ! 'Standardized markup parsers to use a single format: an object. Instantiate
|
26
81
|
a specific markup class with text to output formated Html. Allows for easy code
|
27
82
|
block highlighting using a Proc; defaults to Uv (ruby Ultraviolet)'
|