fop_lang 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +76 -34
- data/bin/fop +11 -0
- data/lib/fop/cli.rb +7 -3
- data/lib/fop/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 798fd7c335f394e878fba2f70a9f60372ea356c79f2dc63392398920d0ffce38
|
4
|
+
data.tar.gz: 654786ff77823e8d8dd9a348f958828346e3755e43a04a0f38e711a6c5571ea9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6761f3d7dd602d1c93a2387fc73ea14c11484e88d0d319bbf87df98925977aa15de59a63f23aafffafa384ce3b9def9f81edabae669aabc2012b00d3131e46f4
|
7
|
+
data.tar.gz: 7f5187cd510d691dda996284d5a400804b7573f67506701e39a6d2909c8a4026b58655f6b2800708e911377ccce790885a2238eed7a75d4873e4b599d23e67df
|
data/README.md
CHANGED
@@ -1,57 +1,99 @@
|
|
1
1
|
# fop_lang
|
2
2
|
|
3
|
-
Fop (Filter and OPerations language) is
|
3
|
+
Fop (Filter and OPerations language) is a tiny, experimental language for filtering and transforming text. Think of it like awk but with the condition and action segments combined.
|
4
4
|
|
5
|
-
|
6
|
-
gem 'fop_lang'
|
7
|
-
```
|
5
|
+
This is a Ruby implementation with both a library interface and a bin command.
|
8
6
|
|
9
|
-
##
|
7
|
+
## Installation
|
10
8
|
|
11
|
-
|
9
|
+
```bash
|
10
|
+
$ gem install fop_lang
|
11
|
+
```
|
12
|
+
|
13
|
+
You may use fop in a Ruby script:
|
12
14
|
|
13
15
|
```ruby
|
14
|
-
|
16
|
+
require 'fop_lang'
|
15
17
|
|
16
|
-
|
17
|
-
=> 'release-5.100.0'
|
18
|
+
f = Fop('foo {N+1}')
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
f.apply('foo 1')
|
21
|
+
=> "foo 2"
|
22
|
+
|
23
|
+
f.apply('bar 1')
|
24
|
+
=> nil
|
25
|
+
```
|
26
|
+
|
27
|
+
or run `fop` from the command line:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
$ echo 'foo 1' | fop 'foo {N+1}'
|
31
|
+
foo 2
|
32
|
+
$ echo 'bar 1' | fop 'foo {N+1}'
|
22
33
|
```
|
23
34
|
|
24
|
-
##
|
35
|
+
## Syntax
|
36
|
+
|
37
|
+
`Text /(R|r)egex/ {N+1}`
|
38
|
+
|
39
|
+
The above program demonstrates a text match, a regex match, and a match expression. If the input matches all three segments, output is given. If the input was `Text regex 5`, the output would be `Text regex 6`.
|
40
|
+
|
41
|
+
### Text match
|
42
|
+
|
43
|
+
The input must match this text exactly. Whitespace is part of the match. Wildcards (`*`) are allowed. Special characters (`*/{}\`) may be escaped with `\`.
|
25
44
|
|
26
|
-
|
45
|
+
The output of a text match will be the matching input.
|
27
46
|
|
28
|
-
|
47
|
+
### Regex match
|
29
48
|
|
30
|
-
|
49
|
+
Regular expressions may be placed between `/`s. If the regular expression contains a `/`, you may escape it with `\`. Special regex characters like `[]()+.*` may also be escaped with `\`.
|
31
50
|
|
32
|
-
|
51
|
+
The output of a regex match will be the matching input.
|
33
52
|
|
34
|
-
|
53
|
+
### Match expression
|
35
54
|
|
36
|
-
|
55
|
+
A match expression both matches on input and modifies that input. An expression is made up of 1 - 3 parts:
|
37
56
|
|
38
|
-
|
57
|
+
1. The match, e.g. `N` for numeric.
|
58
|
+
2. The operator, e.g. `+` for addition (optional).
|
59
|
+
3. The argument, e.g `1` for "add one" (required for most operators).
|
39
60
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
61
|
+
The output of a match expression will be the _modified_ matching input. If no operator is given, the output will be the matching input.
|
62
|
+
|
63
|
+
**Matches**
|
64
|
+
|
65
|
+
* `N` matches one or more consecutive digits.
|
66
|
+
* `A` matches one or more letters (lower or upper case).
|
67
|
+
* `W` matches alphanumeric chars and underscores.
|
68
|
+
* `*` greedily matches everything after it.
|
69
|
+
* `/regex/` matches on the supplied regex. Capture groups may be referenced in the argument as `$1`, `$2`, etc.
|
70
|
+
|
71
|
+
**Operators**
|
72
|
+
|
73
|
+
* `=` Replace the matching character(s) with the given argument. If no argument is given, drop the matching chars.
|
74
|
+
* `>` Append the argument to the matching value.
|
75
|
+
* `<` Prepend the argument to the matching value.
|
76
|
+
* `+` Perform addition on the matching number and the argument (`N` only).
|
77
|
+
* `-` Subtract the argument from the matching number (`N` only).
|
78
|
+
|
79
|
+
## Examples
|
80
|
+
|
81
|
+
### Release Number Example
|
82
|
+
|
83
|
+
This example takes in GitHub branch names, decides if they're release branches, and if so, increments the version number.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
f = Fop('release-{N}.{N+1}.{N=0}')
|
87
|
+
|
88
|
+
puts f.apply('release-5.99.1')
|
89
|
+
=> 'release-5.100.0'
|
90
|
+
|
91
|
+
puts f.apply('release-5')
|
92
|
+
=> nil
|
93
|
+
# doesn't match the pattern
|
94
|
+
```
|
53
95
|
|
54
|
-
|
96
|
+
### More Examples
|
55
97
|
|
56
98
|
```ruby
|
57
99
|
f = Fop('release-{N=5}.{N+1}.{N=0}')
|
data/bin/fop
CHANGED
@@ -7,7 +7,18 @@ require 'fop_lang'
|
|
7
7
|
require 'fop/cli'
|
8
8
|
|
9
9
|
opts = Fop::CLI.options!
|
10
|
+
|
11
|
+
if opts.version
|
12
|
+
puts Fop::VERSION
|
13
|
+
exit 0
|
14
|
+
end
|
15
|
+
|
10
16
|
src = opts.src.read.chomp
|
17
|
+
if src.empty?
|
18
|
+
$stderr.puts "No expression given"
|
19
|
+
exit 1
|
20
|
+
end
|
21
|
+
|
11
22
|
fop, errors = Fop.compile(src)
|
12
23
|
opts.src.close
|
13
24
|
NL = "\n".freeze
|
data/lib/fop/cli.rb
CHANGED
@@ -2,7 +2,7 @@ require 'optparse'
|
|
2
2
|
|
3
3
|
module Fop
|
4
4
|
module CLI
|
5
|
-
Options = Struct.new(:src, :check, :quiet)
|
5
|
+
Options = Struct.new(:src, :check, :quiet, :version)
|
6
6
|
|
7
7
|
def self.options!
|
8
8
|
options = Options.new
|
@@ -18,12 +18,16 @@ module Fop
|
|
18
18
|
options.check = true
|
19
19
|
end
|
20
20
|
|
21
|
-
opts.on("-q", "--quiet") do
|
21
|
+
opts.on("-q", "--quiet", "Only print errors and output") do
|
22
22
|
options.quiet = true
|
23
23
|
end
|
24
|
+
|
25
|
+
opts.on("--version", "Print version and exit") do
|
26
|
+
options.version = true
|
27
|
+
end
|
24
28
|
end.parse!
|
25
29
|
|
26
|
-
options.src ||= StringIO.new(ARGV.shift)
|
30
|
+
options.src ||= StringIO.new(ARGV.shift || "")
|
27
31
|
options
|
28
32
|
end
|
29
33
|
end
|
data/lib/fop/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fop_lang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A micro expression language for Filter and OPerations on text
|
14
14
|
email: jordan.hollinger@gmail.com
|