schizm 0.0.1 → 0.0.2

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/lib/schizm/markup.rb CHANGED
@@ -1,157 +1,189 @@
1
+ # Copyright (c) 2017 M. Grady Saunders
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions
5
+ # are met:
6
+ #
7
+ # 1. Redistributions of source code must retain the above
8
+ # copyright notice, this list of conditions and the following
9
+ # disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above
12
+ # copyright notice, this list of conditions and the following
13
+ # disclaimer in the documentation and/or other materials
14
+ # provided with the distribution.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18
+ # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24
+ # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
1
28
  require_relative 'string'
2
29
 
3
30
  module Schizm
4
31
 
5
- class Markup
32
+ class Markup
6
33
 
7
- def initialize tag = nil
8
- @tag = tag
9
- @attrs = {}
10
- @elems = []
11
- end
34
+ def initialize tag = nil
35
+ @tag = tag
36
+ @attrs = {}
37
+ @elems = []
38
+ end
12
39
 
13
- attr_reader :tag
14
- attr_accessor :attrs
15
- attr_accessor :elems
40
+ attr_reader :tag
41
+ attr_accessor :attrs
42
+ attr_accessor :elems
16
43
 
17
- def [] key
18
- return @attrs[key]
19
- end
44
+ def [] key
45
+ return @attrs[key]
46
+ end
20
47
 
21
- def []= key, value
22
- return @attrs[key] = value
23
- end
48
+ def []= key, value
49
+ return @attrs[key] = value
50
+ end
24
51
 
25
- def has_attr? name
26
- return @attrs.has_key? name
27
- end
52
+ def get_attr name
53
+ return @attrs[key] if @attrs.has_key? name
54
+ return nil
55
+ end
28
56
 
29
- def add_attrs pairs
30
- pairs.each do |key, value|
31
- @attrs[key] = value
32
- end
33
- return self
57
+ def has_attr? name
58
+ return @attrs.has_key? name
59
+ end
60
+
61
+ def add_attrs pairs
62
+ pairs.each do |key, value|
63
+ @attrs[key] = value
34
64
  end
65
+ return self
66
+ end
35
67
 
36
- def << param
37
- case param
38
- when nil
39
- # Ignore silently.
40
- when ::Array
41
- for entry in param
42
- self << entry
43
- end
44
- when ::Integer
45
- self << '&#' << param.to_s << ';'
46
- when ::String
47
- target = @elems.last
48
- target = @elems unless target.is_a? ::String
49
- target << param
50
- else
51
- @elems << param
68
+ def << param
69
+ case param
70
+ when nil
71
+ # Ignore silently.
72
+ when ::Array
73
+ for entry in param
74
+ self << entry
52
75
  end
53
- return self
76
+ when ::Integer
77
+ self << '&#' << param.to_s << ';'
78
+ when ::String
79
+ target = @elems.last
80
+ target = @elems unless target.is_a? ::String
81
+ target << param
82
+ else
83
+ @elems << param
54
84
  end
85
+ return self
86
+ end
55
87
 
56
- def add_elems param
57
- return self << param
58
- end
88
+ def add_elems param
89
+ return self << param
90
+ end
59
91
 
60
- def last
61
- return @elems.last
62
- end
92
+ def last
93
+ return @elems.last
94
+ end
63
95
 
64
- def yield_each *tags, &block
65
- if @tag and tags.include? @tag.to_s
66
- yield self
67
- end
68
- for elem in @elems
69
- elem.yield_each *tags, &block if elem.is_a? Markup
70
- end
96
+ def yield_each *tags, &block
97
+ if @tag and tags.include? @tag.to_s
98
+ yield self
99
+ end
100
+ for elem in @elems
101
+ elem.yield_each *tags, &block if elem.is_a? Markup
71
102
  end
103
+ end
72
104
 
73
- def find_first_of *tags, &block
74
- if @tag and tags.include? @tag.to_s
75
- return self if not block or (block.call(self) == true)
76
- end
77
- for elem in @elems
78
- temp = elem.find_first_of *tags, &block if elem.is_a? Markup
79
- return temp if temp
80
- end
81
- nil
105
+ def find_first_of *tags, &block
106
+ if @tag and tags.include? @tag.to_s
107
+ return self if not block or (block.call(self) == true)
108
+ end
109
+ for elem in @elems
110
+ temp = elem.find_first_of *tags, &block if elem.is_a? Markup
111
+ return temp if temp
82
112
  end
113
+ nil
114
+ end
83
115
 
84
- def to_plain_text
85
- text = String.new
86
- @elems.each do |elem|
87
- case elem
88
- when Markup
89
- if elem.tag == "script" and
90
- (elem["type"] == "math/tex; mode=inline" or
91
- elem["type"] == "math/tex; mode=display")
92
- temp = elem.elems[0].dump
93
- text << String.new(temp[1..(temp.size-2)]).html
94
- else
95
- text << elem.to_plain_text
96
- end
116
+ def to_plain_text
117
+ text = String.new
118
+ @elems.each do |elem|
119
+ case elem
120
+ when Markup
121
+ if elem.tag == "script" and
122
+ (elem["type"] == "math/tex; mode=inline" or
123
+ elem["type"] == "math/tex; mode=display")
124
+ temp = elem.elems[0].dump
125
+ text << String.new(temp[1..(temp.size-2)]).html
97
126
  else
98
- text << elem.to_s if elem.respond_to? :to_s
127
+ text << elem.to_plain_text
99
128
  end
129
+ else
130
+ text << elem.to_s if elem.respond_to? :to_s
100
131
  end
101
- text
102
132
  end
133
+ text
134
+ end
103
135
 
104
- def to_s
105
- s = String.new
106
- unless @tag == nil
107
- s << "<" << @tag
108
- @attrs.each do |k, v|
109
- if k and k.to_s != ""
110
- s << " #{k.to_s}" if v == nil
111
- s << " #{k.to_s}=\"#{v.to_s}\"" if v != nil and v.to_s != ""
112
- end
136
+ def to_s
137
+ s = String.new
138
+ unless @tag == nil
139
+ s << "<" << @tag
140
+ @attrs.each do |k, v|
141
+ if k and k.to_s != ""
142
+ s << " #{k.to_s}" if v == nil
143
+ s << " #{k.to_s}=\"#{v.to_s}\"" if v != nil and v.to_s != ""
113
144
  end
114
- s << ">"
115
- end
116
- @elems.each do |elem|
117
- s << elem.to_s if elem.respond_to? :to_s
118
- end
119
- unless @tag == nil
120
- s << "</" << @tag << ">"
121
145
  end
122
- s
146
+ s << ">"
123
147
  end
124
-
125
- def guess_title
126
- elem = find_first_of "h1" do |temp|
127
- true if temp.has_attr? "class" and temp["class"] == "title"
128
- end
129
- return elem.to_plain_text if elem
130
- return ""
148
+ @elems.each do |elem|
149
+ s << elem.to_s if elem.respond_to? :to_s
150
+ end
151
+ unless @tag == nil
152
+ s << "</" << @tag << ">"
131
153
  end
154
+ s
155
+ end
132
156
 
133
- def guess_description
134
- size = 80
135
- elem = find_first_of "p"
136
- text = elem.to_plain_text if elem
137
- return text.truncate size if text
138
- return ""
157
+ def guess_title
158
+ elem = find_first_of "h1" do |temp|
159
+ true if temp.has_attr? "class" and temp["class"] == "title"
139
160
  end
161
+ return elem.to_plain_text if elem
162
+ return ""
163
+ end
140
164
 
141
- def guess_parts
142
- parts = "parts:\n"
143
- yield_each "h2" do |elem|
144
- if elem.has_attr? "class"
145
- if elem["class"] == "title"
146
- parts << " -\n"
147
- parts << " id: \"#{elem["id"]}\"\n"
148
- parts << " title: \"#{elem.to_plain_text}\"\n"
149
- end
150
- end
165
+ def guess_description
166
+ size = 80
167
+ elem = find_first_of "p"
168
+ text = elem.to_plain_text if elem
169
+ return text.truncate size if text
170
+ return ""
171
+ end
172
+
173
+ def guess_parts
174
+ tab = " "
175
+ parts = "parts:\n"
176
+ yield_each "h2" do |elem|
177
+ if elem.get_attr "class" == "title" and
178
+ elem.get_attr "id" != nil
179
+ parts.concat "#{tab}-\n"
180
+ parts.concat "#{tab}#{tab}id: \"#{elem.get_attr "id"}\"\n"
181
+ parts.concat "#{tab}#{tab}title: \"#{elem.to_plain_text}\"\n"
151
182
  end
152
- parts
153
183
  end
154
-
184
+ parts
155
185
  end
156
186
 
157
- end
187
+ end # class Markup
188
+
189
+ end # module Schizm
data/lib/schizm/match.rb CHANGED
@@ -1,3 +1,30 @@
1
+ # Copyright (c) 2017 M. Grady Saunders
2
+ #
3
+ # Redistribution and use in source and binary forms, with or without
4
+ # modification, are permitted provided that the following conditions
5
+ # are met:
6
+ #
7
+ # 1. Redistributions of source code must retain the above
8
+ # copyright notice, this list of conditions and the following
9
+ # disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above
12
+ # copyright notice, this list of conditions and the following
13
+ # disclaimer in the documentation and/or other materials
14
+ # provided with the distribution.
15
+ #
16
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18
+ # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24
+ # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
1
28
  module Schizm
2
29
 
3
30
  # Header path, extension put in match group 1.
@@ -208,4 +235,22 @@ module Schizm
208
235
  [/\|==>/u, 10503] # Right double arrow from bar.
209
236
  ]
210
237
 
238
+ CPP_HIGHLIGHT = [
239
+ ["hi-comment", /\/\/.*$/u],
240
+ ["hi-comment", /\/\*.*?\*\//um],
241
+ ["hi-preproc", /\#(?:ifn?def|endif|if|else|elif|define|pragma|include)\b.*?(?<=[^\\])(?:\n|\z)/um],
242
+ ["hi-type", /(?:auto|bool|char|short|long|int|(?:un)?signed|float|double|void)\b/u],
243
+ ["hi-type", /(?:size|ptrdiff|char(?:16|32)|wchar|u?int(?:_least|_fast)?(?:8|16|32|64))_t\b/u],
244
+ ["hi-type-spec", /(?:constexpr|const|volatile|static|mutable|thread_local|register|extern|inline|noexcept)\b/u],
245
+ ["hi-flow", /(?:new|delete|do|while|for|if|else|try|throw|catch|switch|case|default|break|continue|goto|return)\b/u],
246
+ ["hi-op", /(?:(?:and|or|xor|not)(?:_eq)?|bitand|bitor|compl|(?:(?:const|dynamic|static|reinterpret)_cast))\b/u],
247
+ ["hi-value", /(?:true|false|this|nullptr|NULL)\b/u],
248
+ ["hi-value", /'[^'\\]*(?:\\.[^'\\]*)*'/u], # Single quote
249
+ ["hi-value", /"[^"\\]*(?:\\.[^"\\]*)*"/u], # Double quote
250
+ ["hi-other", /(?:alignas|alignof|sizeof|decltype|template|typename|typedef|using|concept|requires|operator)\b/u],
251
+ ["hi-other", /(?:namespace|class|struct|enum|public|protected|private|friend|final|virtual|override)\b/u],
252
+ ["hi-ident", /[a-zA-Z]\w*/u],
253
+ [nil, /[^\w\s]+/um]
254
+ ]
255
+
211
256
  end # module Schizm