textpow1x 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ before_script:
2
+ - sudo apt-get install -y libonig-dev && bundle install
3
+ script: "bundle exec rake"
4
+ rvm:
5
+ - ree
6
+ - 1.9.2
7
+ - 1.9.3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- textpow1x (1.0.0)
4
+ textpow1x (1.1.0)
5
5
  plist (>= 3.0.1)
6
6
 
7
7
  GEM
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,18 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining
2
+ a copy of this software and associated documentation files (the
3
+ 'Software'), to deal in the Software without restriction, including
4
+ without limitation the rights to use, copy, modify, merge, publish,
5
+ distribute, sublicense, and/or sell copies of the Software, and to
6
+ permit persons to whom the Software is furnished to do so, subject to
7
+ the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be
10
+ included in all copies or substantial portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
13
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
15
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
16
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
17
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- A library for parsing {TextMate}[http://macromates.com/] bundles.
1
+ Read {TextMate}[http://macromates.com/] syntax files and parse text with them.
2
2
 
3
3
  For Ruby 1.8 and 1.9.
4
4
 
@@ -31,180 +31,53 @@ Install oniguruma
31
31
 
32
32
  3. Parse some text:
33
33
 
34
- syntax.parse( text, processor )
35
-
36
-
37
- === INDEPTH:
38
-
39
- At the heart of syntax parsing are ..., well, syntax files. Lets see for instance
40
- the example syntax that appears in textmate's
41
- {documentation}[http://macromates.com/textmate/manual/language_grammars#language_grammars]:
42
-
43
-
44
- { scopeName = 'source.untitled';
45
- fileTypes = ( txt );
46
- foldingStartMarker = '\{\s*$';
47
- foldingStopMarker = '^\s*\}';
48
- patterns = (
49
- { name = 'keyword.control.untitled';
50
- match = '\b(if|while|for|return)\b';
51
- },
52
- { name = 'string.quoted.double.untitled';
53
- begin = '"';
54
- end = '"';
55
- patterns = (
56
- { name = 'constant.character.escape.untitled';
57
- match = '\\.';
58
- }
59
- );
60
- },
61
- );
62
- }
63
-
64
- But Textpow is not able to parse text pfiles. However, in practice this is not a problem,
65
- since it is possible to convert both text and binary pfiles to an XML format. Indeed, all
66
- the syntaxes in the Textmate syntax {repository}[http://macromates.com/svn/Bundles/trunk/Bundles/]
67
- are in XML format:
68
-
69
- <?xml version="1.0" encoding="UTF-8"?>
70
- <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
71
- <plist version="1.0">
72
- <dict>
73
- <key>scopeName</key>
74
- <string>source.untitled</string>
75
- <key>fileTypes</key>
76
- <array>
77
- <string>txt</string>
78
- </array>
79
- <key>foldingStartMarker</key>
80
- <string>\{\s*$</string>
81
- <key>foldingStopMarker</key>
82
- <string>^\s*\}</string>
83
- <key>patterns</key>
84
- <array>
85
- <dict>
86
- <key>name</key>
87
- <string>keyword.control.untitled</string>
88
- <key>match</key>
89
- <string>\b(if|while|for|return)\b</string>
90
- </dict>
91
- <dict>
92
- <key>name</key>
93
- <string>string.quoted.double.untitled</string>
94
- <key>begin</key>
95
- <string>"</string>
96
- <key>end</key>
97
- <string>"</string>
98
- <key>patterns</key>
99
- <array>
100
- <dict>
101
- <key>name</key>
102
- <string>constant.character.escape.untitled</string>
103
- <key>match</key>
104
- <string>\\.</string>
105
- </dict>
106
- </array>
107
- </dict>
108
- </array>
109
- </dict>
110
-
111
- Of course, most people find XML both ugly and cumbersome. Fortunately, it is
112
- also possible to store syntax files in YAML format, which is much easier to
113
- read:
114
-
115
- ---
116
- fileTypes:
117
- - txt
118
- scopeName: source.untitled
119
- foldingStartMarker: \{\s*$
120
- foldingStopMarker: ^\s*\}
121
- patterns:
122
- - name: keyword.control.untitled
123
- match: \b(if|while|for|return)\b
124
- - name: string.quoted.double.untitled
125
- begin: '"'
126
- end: '"'
127
- patterns:
128
- - name: constant.character.escape.untitled
129
- match: \\.
130
-
131
- ==== Processors
132
-
133
- Until now we have talked about the parsing process without explaining what
134
- it is exactly. Basically, parsing consists in reading text from a string or
135
- file and applying tags to parts of the text according to what has been
136
- specified in the syntax file.
137
-
138
- In textpow, the process takes place line by line, from the beginning to the
139
- end and from left to right for every line. As the text is parsed, events are
140
- sent to a processor object when a tag is open or closed and so on.
141
- A processor is any object which implements one or more of the following
142
- methods:
143
-
144
- class Processor
145
- def open_tag name, position
146
- end
147
-
148
- def close_tag name, position
149
- end
150
-
151
- def new_line line
152
- end
153
-
154
- def start_parsing name
155
- end
156
-
157
- def end_parsing name
158
- end
34
+ syntax.parse(text, processor)
35
+
36
+ === Syntax-files
37
+
38
+ Syntax files are written with {Textmate grammer}[http://macromates.com/textmate/manual/language_grammars#language_grammars].
39
+ They are written in text/binary/xml/yaml format (all can be translated into each other).
40
+ Textpow can understand the yaml form (ending in .syntax) and the xml form (ending in .tmSyntax or .plist) via plist library, but not the text or binary form.
41
+ All syntax files shipped with textpow are in the yaml form, since its easiest to read and less verbose.
42
+
43
+ Adding a new syntax:
44
+ * use <tt>rake convert[filename]</tt> to convert it into yaml format
45
+ * drop it into the lib/textpow/syntax folder
46
+ * run tests <tt>rake</tt> to see if the syntax is parseable
47
+
48
+ === Processors
49
+ A processors is a hook that is called while parsing a text.
50
+ It must implement these methods:
51
+
52
+ class MyProcessor
53
+ def open_tag(tag_name, position_in_current_line); end
54
+ def close_tag(tag_name, position_in_current_line); end
55
+
56
+ # called before processing a line
57
+ def new_line(line_content); end
58
+
59
+ # called before parsing anything
60
+ def start_parsing(scope_name); end
61
+
62
+ # called before parsing everything
63
+ def end_parsing(scope_name); end
159
64
  end
160
65
 
161
- * <tt>open_tag</tt>. Is called when a new tag is opened, it receives the tag's name and
162
- its position (relative to the current line).
163
- * <tt>close_tag</tt>. The same that <tt>open_tag</tt>, but it is called when a tag is closed.
164
- * <tt>new_line</tt>. Is called every time that a new line is processed, it receives the
165
- line's contents.
166
- * <tt>start_parsing</tt>. Is called once at the beginning of the parsing process. It
167
- receives the scope name for the syntax being used.
168
- * <tt>end_parsing</tt>. Is called once after all the input text has been parsed. It
169
- receives the scope name for the syntax being used.
170
-
171
- Textpow ensures that the methods are called in parsing order, thus,
172
- for example, if there are two subsequent calls to <tt>open_tag</tt>, the first
66
+ Textpow ensures that the methods are called in parsing order.
67
+ If there are two subsequent calls to <tt>open_tag</tt>, the first
173
68
  having <tt>name="text.string", position=10</tt> and the second having
174
- <tt>name="markup.string", position=10</tt>, it should be understood that the
69
+ <tt>name="markup.string", position=10</tt>, it means that the
175
70
  <tt>"markup.string"</tt> tag is inside the <tt>"text.string"</tt> tag.
176
71
 
177
- == CREDITS:
72
+ == TODO
178
73
 
179
- * {Michael Grosser}[http://github.com/grosser]
180
- * {Chris Hoffman}[http://github.com/cehoffman]
181
- * {Spox}[http://github.com/spox]
182
- * Dizan Vasque
74
+ * parse text plist files {example}[https://raw.github.com/kangax/textmate-js-language-syntax-file/master/JavaScript.plist]
75
+ * parse tmLanguage files {example}[https://github.com/mattfarina/Ruby-Sass/blob/master/Ruby%20Sass.tmLanguage]
76
+ * document / spec / fix include languages (they dont work very well)
183
77
 
184
- == LICENSE:
78
+ == LICENSE: MIT
185
79
 
186
- (The MIT License)
187
-
188
- * Copyright (c) 2011 Michael Grosser
189
- * Copyright (c) 2010 Chris Hoffman
190
- * Copyright (c) 2009 Spox
80
+ * Copyright (c) 2011 {Michael Grosser}[http://github.com/grosser]
81
+ * Copyright (c) 2010 {Chris Hoffman}[http://github.com/cehoffman]
82
+ * Copyright (c) 2009 {Spox}[http://github.com/spox]
191
83
  * Copyright (c) 2007-2008 Dizan Vasquez
192
-
193
- Permission is hereby granted, free of charge, to any person obtaining
194
- a copy of this software and associated documentation files (the
195
- 'Software'), to deal in the Software without restriction, including
196
- without limitation the rights to use, copy, modify, merge, publish,
197
- distribute, sublicense, and/or sell copies of the Software, and to
198
- permit persons to whom the Software is furnished to do so, subject to
199
- the following conditions:
200
-
201
- The above copyright notice and this permission notice shall be
202
- included in all copies or substantial portions of the Software.
203
-
204
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
205
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
206
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
207
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
208
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
209
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
210
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -4,3 +4,25 @@ Bundler::GemHelper.install_tasks
4
4
  task :default do
5
5
  sh "rspec spec/"
6
6
  end
7
+
8
+ desc "Convert a plist to a .syntax file"
9
+ task :convert, :file do |t,args|
10
+ require 'plist'
11
+ require 'yaml'
12
+ yaml = Plist.parse_xml(args[:file]).to_yaml
13
+ File.open('out.syntax','w'){|f| f.write(yaml) }
14
+ end
15
+
16
+ rule /^version:bump:.*/ do |t|
17
+ sh "git status | grep 'nothing to commit'" # ensure we are not dirty
18
+ index = ['major', 'minor','patch'].index(t.name.split(':').last)
19
+ file = 'lib/textpow/version.rb'
20
+
21
+ version_file = File.read(file)
22
+ old_version, *version_parts = version_file.match(/(\d+)\.(\d+)\.(\d+)/).to_a
23
+ version_parts[index] = version_parts[index].to_i + 1
24
+ new_version = version_parts * '.'
25
+ File.open(file,'w'){|f| f.write(version_file.sub(old_version, new_version)) }
26
+
27
+ sh "bundle && git add #{file} Gemfile.lock && git commit -m 'bump version to #{new_version}'"
28
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'textpow'
3
+
4
+ syntax = Textpow::SyntaxNode.load("#{Textpow.syntax_path}/javascript.syntax")
5
+ text = File.read('examples/jquery.js')
6
+ processor = Textpow::RecordingProcessor.new
7
+
8
+ start = Time.now.to_f
9
+ 5.times{ syntax.parse(text, processor) }
10
+ puts Time.now.to_f - start
11
+