regex 1.0.0 → 1.1.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/HISTORY +18 -1
- data/LICENSE +202 -21
- data/PACKAGE +7 -0
- data/PROFILE +20 -0
- data/README +76 -21
- data/bin/regex +1 -1
- data/lib/regex.rb +19 -228
- data/lib/regex/command.rb +16 -97
- data/lib/regex/extractor.rb +481 -0
- data/lib/regex/package.yml +7 -0
- data/lib/regex/replacer.rb +221 -0
- data/lib/regex/string.rb +1 -1
- data/lib/regex/templates.rb +85 -0
- data/{test/demos → qed}/regex.rdoc +5 -5
- data/qed/replacer.rdoc +57 -0
- metadata +54 -29
- data/MANIFEST +0 -25
- data/lib/regex/templates/common.rb +0 -13
- data/meta/authors +0 -2
- data/meta/created +0 -1
- data/meta/description +0 -1
- data/meta/download +0 -1
- data/meta/homepage +0 -1
- data/meta/mailinglist +0 -1
- data/meta/name +0 -1
- data/meta/repository +0 -1
- data/meta/summary +0 -1
- data/meta/title +0 -1
- data/meta/version +0 -1
data/HISTORY
CHANGED
@@ -1,6 +1,23 @@
|
|
1
1
|
= RELEASE HISTORY
|
2
2
|
|
3
|
-
1.
|
3
|
+
== 1.1.0 / 2010-10-12
|
4
|
+
|
5
|
+
This release adds a detailed output option, and corrects
|
6
|
+
a bug when using `--escape` with search and replace. It also
|
7
|
+
entails a pretty extensive under-the-hood overhaul of the
|
8
|
+
Extractor class. One consequence of this overhaul is that the
|
9
|
+
`--unxml` option has been deprecated until such time that it can
|
10
|
+
be reimplemented correctly.
|
11
|
+
|
12
|
+
Changes:
|
13
|
+
|
14
|
+
* Add --detail/-d output option.
|
15
|
+
* Fix isssue using escape with search and replace.
|
16
|
+
* Reimplement Extractor class.
|
17
|
+
* Deprecate `--unxml` option until implementation can be worked out.
|
18
|
+
|
19
|
+
|
20
|
+
== 1.0.0 / 2010-02-10
|
4
21
|
|
5
22
|
Initial release of Regex. Regex is a simple
|
6
23
|
commandline Regular Expression tool.
|
data/LICENSE
CHANGED
@@ -1,23 +1,204 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
.
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
22
203
|
|
23
204
|
|
data/PACKAGE
ADDED
data/PROFILE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
---
|
2
|
+
title : Regex
|
3
|
+
summary: Regex is a simple commmandline Regular Expression tool.
|
4
|
+
created: 2006-05-09
|
5
|
+
|
6
|
+
authors:
|
7
|
+
- Thomas Sawyer
|
8
|
+
- Tyler Rick
|
9
|
+
|
10
|
+
description:
|
11
|
+
Regex is a simple commmandline Regular Expression tool,
|
12
|
+
that makes easy to search documents for content matches.
|
13
|
+
|
14
|
+
resources:
|
15
|
+
homepage: http://proutils.github.com/regex
|
16
|
+
repository: git://github.com/proutils/regex.git
|
17
|
+
documentation: http://wiki.github.com/proutils/regex
|
18
|
+
development: http://github.com/proutils/regex
|
19
|
+
download: http://github.com/proutils/regex/downloads
|
20
|
+
mailinglist: http://groups.google.com/group/proutils/topics?hl=en
|
data/README
CHANGED
@@ -1,45 +1,100 @@
|
|
1
|
-
= Regex
|
1
|
+
= Regex
|
2
|
+
|
3
|
+
("Like a Knife")
|
2
4
|
|
3
|
-
* home: http://proutils.github.com/regex
|
4
|
-
* work: http://github.com/proutils/regex
|
5
5
|
|
6
6
|
== DESCRIPTION
|
7
7
|
|
8
|
-
Yea, I know what you are going to say. "I can do that with
|
8
|
+
Yea, I know what you are going to say. "I can do that with ____" Fill in the blank
|
9
9
|
with +grep+, +awk+, +sed+, +perl+, etc. But honestly, none of these tools are
|
10
|
-
Langauge 2.0 (read "post-Ruby"). What I want is a
|
11
|
-
|
10
|
+
"Langauge 2.0" (read "post-Ruby"). What I want is a simple command-line tool that
|
11
|
+
gives me quick access to a Regular Expression engine. No more, no less.
|
12
12
|
|
13
|
-
Now I could have written this
|
13
|
+
Now I could have written this in Perl. I'm sure it would just as good, if not
|
14
14
|
better since Perl's Regualar Expression engine rocks, or so I hear. But Ruby's is
|
15
|
-
pretty good too, and getting better (with 1.9+). And since I know Ruby very
|
16
|
-
well. Well that's
|
15
|
+
pretty damn good too, and getting better (with 1.9+). And since I know Ruby very
|
16
|
+
well. Well that's what you get.
|
17
|
+
|
18
|
+
|
19
|
+
== RESOURCES
|
20
|
+
|
21
|
+
* Home: http://rubyworks.github.com/regex
|
22
|
+
* Code: http://github.com/rubyworks/regex
|
23
|
+
|
17
24
|
|
18
25
|
== USAGE
|
19
26
|
|
20
|
-
|
21
|
-
|
27
|
+
For detailed explication and examples of usage refer to the
|
28
|
+
(User Docs){http://wiki.github.com/rubyworks/regex}, the
|
29
|
+
(QED Docs){http://github.com/rubyworks/regex/docs/qed} and the
|
30
|
+
(API Docs){http://github.com/rubyworks/regex/docs/api}.
|
31
|
+
|
32
|
+
In brief, usage simply entails supplying a regular expression and a list of files
|
33
|
+
to be searched to the +regex+ command.
|
34
|
+
|
35
|
+
$ regex '/=begin.*?\n(.*)\n=end/' sample.rb
|
36
|
+
|
37
|
+
This example does exactly what you would expect --returns the content between
|
38
|
+
the first <code>=begin ... =end</code> clause it comes across. To see all such
|
39
|
+
block comments, as you would expect, you can use add <code>g</code> regular
|
40
|
+
expression mode flag.
|
41
|
+
|
42
|
+
$ regex '/=begin.*?\n(.*)\n=end/g' sample.rb
|
43
|
+
|
44
|
+
Alternatively you can use the <code>--repeat/--global/-g</code> option.
|
45
|
+
|
46
|
+
$ regex -g '/=begin.*?\n(.*)\n=end/' sample.rb
|
22
47
|
|
23
|
-
|
48
|
+
Notice that in all these examples we have used single quotes to wrap the
|
49
|
+
regular expression. This is to prevent the shell from expanding <code>*</code>
|
50
|
+
and <code>?<code> marks.
|
24
51
|
|
25
|
-
|
52
|
+
By default +regex+ produces string output. Regular expression groups are delimited
|
53
|
+
by ASCII 29 (035 1D) END OF GROUP, and repeat matches are delimited by
|
54
|
+
ASCII character 30 (036 1E) END OF RECORD.
|
26
55
|
|
27
|
-
|
56
|
+
Instead of string output, +regex+ also supports YAML and JSON formats using the
|
57
|
+
<code>--yaml/-y</code> and <code>--json/-j</code> options.
|
58
|
+
|
59
|
+
$ regex -y -g '/=begin.*?\n(.*)\n=end/' sample.rb
|
60
|
+
|
61
|
+
In this case the returned matches are delimited using as an array of arrays.
|
62
|
+
|
63
|
+
To get more information that just the match results use the <code>--detail/-d</code>
|
64
|
+
option.
|
65
|
+
|
66
|
+
Also, we can do without the the <code>/ /<code> deliminators on the regular
|
67
|
+
expression if we use the <code>--search/-s</code> option instead. Going back to
|
68
|
+
our first example:
|
69
|
+
|
70
|
+
$ regex -s '=begin.*?\n(.*)\n=end' sample.rb
|
71
|
+
|
72
|
+
To replace text, use the <code>--replace/--r</code> option.
|
73
|
+
|
74
|
+
$ regex --yaml --repeat -s 'Tom' -r 'Bob' sample.rb
|
75
|
+
|
76
|
+
This will replace every occurance of "Tom" with "Bob" in the <code>sample.rb</code>
|
77
|
+
file. By default <code>regex</code> will backup any file it changes by adding a
|
78
|
+
<code>.bak</code> extension to the original copy.
|
79
|
+
|
80
|
+
Check out the <code>--help</code> and I am sure the rest will be smooth sailing.
|
28
81
|
But it you want more information, then do us the good favor of jumping over
|
29
|
-
to the
|
82
|
+
to the wiki[http://wiki.github.com/rubyworks/regex].
|
30
83
|
|
31
84
|
== STATUS
|
32
85
|
|
33
|
-
This is a very release. So don't expect every feature under the sun just yet,
|
34
|
-
that every detail is going to work peachy
|
35
|
-
or a feature needs adding, well then get in there and send me a patch
|
36
|
-
source software is built on *TEAM WORK
|
86
|
+
This is a very early release. So don't expect every feature under the sun just yet,
|
87
|
+
or that every detail is going to work peachy. But hey, if something needs fixing
|
88
|
+
or a feature needs adding, well then get in there and send me a patch. Open
|
89
|
+
source software is built on *TEAM WORK*, baby.
|
37
90
|
|
38
|
-
|
91
|
+
Expect a potenial for rapid change here at the beginning.
|
39
92
|
|
40
93
|
== COPYRIGHT
|
41
94
|
|
42
95
|
Copyright (c) 2010 Thomas Sawyer
|
43
96
|
|
44
|
-
Regex is licensed under the terms of the
|
97
|
+
Regex is licensed under the terms of the Apache License, Version 2.0.
|
98
|
+
|
99
|
+
See LICENSE file for details.
|
45
100
|
|
data/bin/regex
CHANGED
data/lib/regex.rb
CHANGED
@@ -1,236 +1,27 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
class Regex
|
10
|
-
VERSION = "1.1"
|
11
|
-
|
12
|
-
# When the regular expression return multiple groups,
|
13
|
-
# each is divided by the group deliminator.
|
14
|
-
# This is the default value.
|
15
|
-
DELIMINATOR_GROUP = 29.chr + "\n"
|
16
|
-
|
17
|
-
# When using repeat mode, each match is divided by
|
18
|
-
# the record deliminator. This is the default value.
|
19
|
-
DELIMINATOR_RECORD = 30.chr + "\n"
|
20
|
-
|
21
|
-
require 'fileutils'
|
22
|
-
require 'open-uri'
|
23
|
-
|
24
|
-
require 'regex/string'
|
25
|
-
require 'regex/command'
|
26
|
-
|
27
|
-
# TODO: generalize to plugin
|
28
|
-
require 'regex/templates/common'
|
29
|
-
|
30
|
-
#
|
31
|
-
#attr_accessor :text
|
32
|
-
|
33
|
-
# Remove XML tags from search.
|
34
|
-
attr_accessor :unxml
|
35
|
-
|
36
|
-
# Regular expression.
|
37
|
-
attr_accessor :pattern
|
38
|
-
|
39
|
-
# Select built-in regular expression by name.
|
40
|
-
attr_accessor :template
|
41
|
-
|
42
|
-
# Index of expression return.
|
43
|
-
attr_accessor :index
|
44
|
-
|
45
|
-
# Ignore case.
|
46
|
-
attr_accessor :insensitive
|
47
|
-
|
48
|
-
# Repeat Match.
|
49
|
-
attr_accessor :repeat
|
50
|
-
|
51
|
-
# Output format.
|
52
|
-
attr_accessor :format
|
53
|
-
|
54
|
-
# DEPRECATE: Not needed anymore.
|
55
|
-
#def self.load(io, options={}, &block)
|
56
|
-
# new(io, options, &block)
|
57
|
-
#end
|
58
|
-
|
59
|
-
# New extractor.
|
60
|
-
def initialize(io, options={})
|
61
|
-
@raw = (String === io ? io : io.read)
|
62
|
-
options.each do |k,v|
|
63
|
-
__send__("#{k}=", v)
|
64
|
-
end
|
65
|
-
yield(self) if block_given?
|
66
|
-
end
|
67
|
-
|
68
|
-
# Read file.
|
69
|
-
#def raw
|
70
|
-
# @raw ||= open(@file) # File.read(@file)
|
71
|
-
#end
|
72
|
-
|
73
|
-
#--
|
74
|
-
# TODO: unxml is too primative, use real xml parser like nokogiri
|
75
|
-
#++
|
76
|
-
def text
|
77
|
-
@text ||= (
|
78
|
-
if unxml
|
79
|
-
raw.gsub!(/\<(.*?)\>/, '')
|
80
|
-
else
|
81
|
-
@raw
|
82
|
-
end
|
1
|
+
module Regex
|
2
|
+
DIRECTORY = File.dirname(__FILE__)
|
3
|
+
|
4
|
+
# Access to PACAKGE metadata.
|
5
|
+
def self.package
|
6
|
+
@package ||= (
|
7
|
+
require 'yaml'
|
8
|
+
YAML.load(File.new(DIRECTORY + '/regex/package.yml'))
|
83
9
|
)
|
84
10
|
end
|
85
11
|
|
86
|
-
#
|
87
|
-
def
|
88
|
-
|
89
|
-
if template
|
90
|
-
TEMPLATES.const_get(template.upcase)
|
91
|
-
else
|
92
|
-
case pattern
|
93
|
-
when Regexp
|
94
|
-
pattern
|
95
|
-
when String
|
96
|
-
flags = []
|
97
|
-
flags << Regexp::MULTILINE
|
98
|
-
flags << Regexp::IGNORECASE if insensitive
|
99
|
-
Regexp.new(pattern, *flags)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
)
|
103
|
-
end
|
104
|
-
|
105
|
-
#
|
106
|
-
def to_s(format=nil)
|
107
|
-
case format
|
108
|
-
when :yaml
|
109
|
-
to_s_yaml
|
110
|
-
when :json
|
111
|
-
to_s_json
|
112
|
-
else
|
113
|
-
out = structure
|
114
|
-
if repeat
|
115
|
-
out = out.map{ |m| m.join(deliminator_group) }
|
116
|
-
out = out.join(deliminator_record) #.chomp("\n") + "\n"
|
117
|
-
else
|
118
|
-
out = out.join(deliminator_group) #.chomp("\n") + "\n"
|
119
|
-
end
|
120
|
-
out
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
#
|
125
|
-
def to_s_yaml
|
126
|
-
require 'yaml'
|
127
|
-
structure.to_yaml
|
128
|
-
end
|
129
|
-
|
130
|
-
#
|
131
|
-
def to_s_json
|
132
|
-
begin
|
133
|
-
require 'json'
|
134
|
-
rescue LoadError
|
135
|
-
require 'json_pure'
|
136
|
-
end
|
137
|
-
structure.to_json
|
138
|
-
end
|
139
|
-
|
140
|
-
# Structure the matchdata according to specified options.
|
141
|
-
def structure
|
142
|
-
repeat ? structure_repeat : structure_single
|
143
|
-
end
|
144
|
-
|
145
|
-
# Structure the matchdata for single match.
|
146
|
-
def structure_single
|
147
|
-
md = extract
|
148
|
-
if index
|
149
|
-
[md[index]]
|
150
|
-
elsif md.size > 1
|
151
|
-
md[1..-1]
|
152
|
-
else
|
153
|
-
[md[0]]
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
# Structure the matchdata for repeat matches.
|
158
|
-
def structure_repeat
|
159
|
-
out = extract
|
160
|
-
if index
|
161
|
-
out.map{ |md| [md[index]] }
|
162
|
-
else
|
163
|
-
out.map{ |md| md.size > 1 ? md[1..-1] : [md[0]] }
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
# Extract match from source text.
|
168
|
-
def extract
|
169
|
-
if repeat
|
170
|
-
extract_repeat
|
171
|
-
else
|
172
|
-
extract_single
|
173
|
-
end
|
12
|
+
# Need VRESION? You got it.
|
13
|
+
def self.const_missing(name)
|
14
|
+
package[name.to_s.downcase] || super(name)
|
174
15
|
end
|
175
16
|
|
176
|
-
#
|
177
|
-
|
178
|
-
|
179
|
-
# if md = matchdata
|
180
|
-
# if index
|
181
|
-
# out << md[index]
|
182
|
-
# elsif md.size > 1
|
183
|
-
# out = md[1..-1] #.join(deliminator_group)
|
184
|
-
# else
|
185
|
-
# out = md
|
186
|
-
# end
|
187
|
-
# end
|
188
|
-
# return out
|
189
|
-
#end
|
190
|
-
|
191
|
-
# Extract single match from source text.
|
192
|
-
def extract_single
|
193
|
-
md = regex.match(text)
|
194
|
-
md ? md : []
|
195
|
-
end
|
196
|
-
|
197
|
-
#
|
198
|
-
#def matchdata
|
199
|
-
# regex.match(text)
|
200
|
-
#end
|
201
|
-
|
202
|
-
#
|
203
|
-
#def extract_repeat
|
204
|
-
# out = []
|
205
|
-
# text.scan(regex) do
|
206
|
-
# md = $~
|
207
|
-
# if index
|
208
|
-
# out << [md[index]]
|
209
|
-
# elsif md.size > 1
|
210
|
-
# out << md[1..-1] #.join(deliminator_group)
|
211
|
-
# else
|
212
|
-
# out << md
|
213
|
-
# end
|
214
|
-
# end
|
215
|
-
# out #.join(deliminator_record)
|
216
|
-
#end
|
217
|
-
|
218
|
-
# Extract repeat matches from source text.
|
219
|
-
def extract_repeat
|
220
|
-
out = []
|
221
|
-
text.scan(regex) do
|
222
|
-
out << $~
|
223
|
-
end
|
224
|
-
out
|
17
|
+
# Shortcut to create a new Regex::Extractor instance.
|
18
|
+
def self.new(*io)
|
19
|
+
Extractor.new(*io)
|
225
20
|
end
|
226
|
-
|
227
|
-
def deliminator_group
|
228
|
-
DELIMINATOR_GROUP
|
229
|
-
end
|
230
|
-
|
231
|
-
def deliminator_record
|
232
|
-
DELIMINATOR_RECORD
|
233
|
-
end
|
234
|
-
|
235
21
|
end
|
236
22
|
|
23
|
+
require 'regex/templates'
|
24
|
+
require 'regex/extractor'
|
25
|
+
require 'regex/replacer'
|
26
|
+
require 'regex/command'
|
27
|
+
|