zxc 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8a6facbb820de32db32cb8a5c71221e959deffcc78ce3583be2fd45dfd01bbe
4
+ data.tar.gz: 8332a68e6627a9bba769116b4e0b4a52f47f1f24a7afb33e552032536a21b2f6
5
+ SHA512:
6
+ metadata.gz: 88bb3a737aff502363e517723d1b74b957dfaa81fb94ac0eafcbb92bf8962d3225aab4819c166fb7de9f6dcce02e9a2657900af6e00ebc08c72d030afdef9d88
7
+ data.tar.gz: 1529d74ab9d6cc8b5896336dc5d514ad8aa2217ef41289cc4f76df0e6cfc2a29aed8a030e20fd065e1264d75f2d310bf2a4d29872ea5fef98808ac49de627203
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 tiago-macedo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Zxc
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zxc`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zxc.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/lib/zxc/error.rb ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zxc
4
+ class Error < StandardError
5
+ attr_reader :meta
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zxc
4
+ class UnknownStepError < Error
5
+ def initialize(*args, step:)
6
+ @meta = {step: step.to_s}
7
+ super(*args)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zxc
4
+ class WrongArgumentNumberError < Error
5
+ def initialize(*args, step:, expected:, actual:)
6
+ @meta = {step: step.to_s, expected: expected, actual: actual}
7
+ super(*args)
8
+ end
9
+ end
10
+ end
data/lib/zxc/node.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zxc
4
+ class Node
5
+ attr_accessor :text, :subnodes, :parent, :til_root, :til_leaf
6
+
7
+ def initialize(text, parent)
8
+ @text = text
9
+ @parent = parent
10
+ @subnodes = []
11
+ if parent.nil?
12
+ @til_root = 0
13
+ else
14
+ @til_root = parent.til_root + 1
15
+ end
16
+ @til_leaf = 0
17
+ end
18
+
19
+ def for_subtree(&block)
20
+ yield self
21
+ subnodes.each { |subnode| subnode.for_subtree(&block) }
22
+ end
23
+
24
+ def at_depth(n)
25
+ if n >= 0
26
+ return [self] if @til_root == n
27
+ else
28
+ return [self] if @til_leaf == -1-n
29
+ end
30
+
31
+ list = []
32
+ subnodes.each do |subnode|
33
+ list += subnode.at_depth(n)
34
+ end
35
+
36
+ list
37
+ end
38
+
39
+ def leaves
40
+ at_depth(-1)
41
+ end
42
+
43
+ def short(n)
44
+ return text if text.size <= n
45
+
46
+ text[...(n-3)] + "..."
47
+ end
48
+ end
49
+ end
data/lib/zxc/step.rb ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zxc
4
+ class Step
5
+ Kind = {}
6
+
7
+ def initialize(args)
8
+ @args = args
9
+ end
10
+
11
+ def run(root)
12
+ raise NotImplementeError
13
+ end
14
+
15
+ def depth_increased(root)
16
+ root.for_subtree { |node| node.til_leaf += 1 unless node.subnodes.empty? }
17
+ end
18
+
19
+ def depth_decreased(root)
20
+ root.for_subtree { |node| node.til_leaf -= 1 }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class First < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:first] = [self, 0]
9
+ superclass::Kind[:"1st"] = [self, 0]
10
+
11
+ def run(root)
12
+ root.at_depth(-2).each do |node|
13
+ node.subnodes = [node.subnodes.first]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class Last < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:last] = [self, 0]
9
+ superclass::Kind[:lst] = [self, 0]
10
+
11
+ def run(root)
12
+ root.at_depth(-2).each do |node|
13
+ node.subnodes = [node.subnodes.last]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class Match < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:match] = [self, 1]
9
+ superclass::Kind[:mtc] = [self, 1]
10
+
11
+ def run(root)
12
+ root.leaves.each { |l| run_leaf(l) }
13
+ end
14
+
15
+ def run_leaf(node)
16
+ unless node.text.include?(@args[0])
17
+ node.parent.subnodes.reject! { |sub| sub == node }
18
+ node = nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class MatchRegexp < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:match_regexp] = [self, 1]
9
+ superclass::Kind[:mrg] = [self, 1]
10
+
11
+ def initialize(...)
12
+ super(...)
13
+ @rgxp = Regexp.new(@args[0])
14
+ end
15
+
16
+ def run(root)
17
+ root.leaves.each { |l| run_leaf(l) }
18
+ end
19
+
20
+ def run_leaf(node)
21
+ unless node.text =~ @rgxp
22
+ node.parent.subnodes.reject! { |sub| sub == node }
23
+ node = nil
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class Present < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:present] = [self, 0]
9
+ superclass::Kind[:press] = [self, 0]
10
+ superclass::Kind[:prs] = [self, 0]
11
+
12
+ def run(root)
13
+ root.leaves.each { |l| run_leaf(l) }
14
+ end
15
+
16
+ def run_leaf(node)
17
+ if node.text == nil or node.text.empty?
18
+ node.parent.subnodes.reject! { |sub| sub == node }
19
+ node = nil
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class Print < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:print] = [self, 0]
9
+ superclass::Kind[:prl] = [self, 0]
10
+
11
+ def run(root)
12
+ root.leaves.each { |l| puts l.text }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class PrintTree < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:print_tree] = [self, 0]
9
+ superclass::Kind[:prt] = [self, 0]
10
+
11
+ def run(root)
12
+ print_node(root, 80)
13
+ end
14
+
15
+ def print_node(node, len)
16
+ if node.subnodes.empty?
17
+ puts "#{"│" * node.til_root}─ #{node.short(len)} (#{node.til_root}|#{node.til_leaf})"
18
+ else
19
+ puts "#{"│" * node.til_root}┌ #{node.short(len)} (#{node.til_root}|#{node.til_leaf})"
20
+ end
21
+ node.subnodes.each_with_index do |sub, idx|
22
+ if idx+1 < node.subnodes.size
23
+ print_node(sub, len-1)
24
+ else
25
+ print_node(sub, len-1)
26
+ end
27
+ end
28
+ puts "#{"│" * node.til_root}└" if not node.subnodes.empty?
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class Split < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:split] = [self, 1]
9
+ superclass::Kind[:spl] = [self, 1]
10
+
11
+ def run(root)
12
+ root.leaves.each { |l| run_leaf(l) }
13
+ depth_increased(root)
14
+ end
15
+
16
+ def run_leaf(node)
17
+ splitted = node.text.split(@args[0])
18
+ splitted.each do |t|
19
+ node.subnodes << Node.new(t, node)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Zxc
6
+ class SplitRegexp < Step
7
+ # Register this class as a step
8
+ superclass::Kind[:split_regexp] = [self, 1]
9
+ superclass::Kind[:srg] = [self, 1]
10
+
11
+ def run(root)
12
+ root.leaves.each { |l| run_leaf(l) }
13
+ depth_increased(root)
14
+ end
15
+
16
+ def run_leaf(node)
17
+ splitted = node.text.split(Regexp.new(@args[0]))
18
+ splitted.each do |t|
19
+ node.subnodes << Node.new(t, node)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zxc
4
+ VERSION = "0.1.0"
5
+ end
data/lib/zxc.rb ADDED
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "zxc/version"
4
+ require_relative "./zxc/node.rb"
5
+ require_relative "./zxc/step.rb"
6
+ Dir[__dir__ + "/zxc/steps/*.rb"].each { |f| require_relative f }
7
+ require_relative "./zxc/error.rb"
8
+ Dir[__dir__ + "/zxc/errors/*.rb"].each { |f| require_relative f }
9
+
10
+ module Zxc
11
+ class Zxc
12
+ def initialize
13
+ @steps = self.class.parse_steps(ARGV)
14
+ @root = Node.new(STDIN.read, nil)
15
+ end
16
+
17
+ def run
18
+ @steps.each { |s| s.run(@root) }
19
+ end
20
+
21
+ def self.parse_steps(cmd_args)
22
+ steps = []
23
+
24
+ while cmd_args.size > 0
25
+ inst = cmd_args.shift.to_sym
26
+ step_class, n_args = Step::Kind[inst]
27
+
28
+ raise UnknownStepError.new(step: inst) unless step_class
29
+
30
+ args = cmd_args[...n_args]
31
+
32
+ raise WrongArgumentNumberError.new(
33
+ step: inst,
34
+ expected: n_args,
35
+ actual: args.size
36
+ ) unless n_args == args.size
37
+
38
+ n_args.times.each { cmd_args.shift }
39
+
40
+ steps << step_class.new(args)
41
+ end
42
+
43
+ steps
44
+ end
45
+ end
46
+ end
data/man/zxc.1 ADDED
@@ -0,0 +1,78 @@
1
+ .TH zxc 1
2
+ .SH NAME
3
+ zxc \- manipulate text using a tree
4
+
5
+ .SH SYNOPSIS
6
+
7
+ .B zxc
8
+ [OPTIONS]
9
+ [STEP [ARG]...]...
10
+
11
+ .SH DESCRIPTION
12
+
13
+ zxc reads text from STDIN, converts it into the root node of a tree, and applies operations (steps) onto said tree.
14
+
15
+ .SH OPTIONS
16
+
17
+ .IP
18
+ .BR \-h ",
19
+ .BR \-\-help
20
+ Displays a short help message and exits
21
+ .br
22
+ .BR \-H ",
23
+ .BR \-\-Help
24
+ Displays this page and exits
25
+ .PP
26
+
27
+ .SH STEPS
28
+
29
+ Unless otherwise specified, a step will not increase or decrease the depth of the tree.
30
+
31
+ .SS print, prl
32
+ Prints all leaves, each leaf in a separate line.
33
+ .br
34
+
35
+ .SS print_tree, prt
36
+ Prints a representation of the whole tree.
37
+ .br
38
+
39
+ .SS match, mtc ARG
40
+ Deleting the leaves which do not contain the substring ARG.
41
+
42
+ .SS match_regexp, mrg ARG
43
+ Deletes the leveaves which do not match the regular expression ARG.
44
+ .br
45
+ Regular expressions are use Ruby conventions.
46
+
47
+ .SS split, spl ARG
48
+ Performs the following operation on each leaf:
49
+ .IP
50
+ .B 1.
51
+ splits the node's text into substrings, using ARG as the separator,
52
+ .br
53
+ .B 2.
54
+ creates a new node for each of the generated substrings, setting the original leaf as their parent node.
55
+ .PP
56
+ This step increases the depth od the tree by 1.
57
+
58
+ .SS split_regexp, srg ARG
59
+ Works as
60
+ .BR split ",
61
+ but splits the original string where it matches the regular expression given by ARG.
62
+
63
+ .SS present, press, prs
64
+ Deletes all leaves whose text is an empty string.
65
+
66
+ .SS first, 1st
67
+ On all nodes which are parents of leaves (that is, nodes at the penultimate level pf tree depth), deletes all child nodes except the first one.
68
+ .br
69
+ For example, if you have performed a split, breaking strings into substrings, this step will leave only the first substrings for each string the step acted on.
70
+
71
+ ..SS last, lst
72
+ Works as
73
+ .BR first ",
74
+ but leaves the last leaf intact instead of the first.
75
+
76
+ .SH AUTHORS
77
+
78
+ Tiago Macedo <tiagomacedo@ufrj.br>
data/sig/zxc.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Zxc
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zxc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - tiago-macedo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-07-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ `zxc` allows you to quickly perform a few common opperations on STDIN, for example:
15
+ ```
16
+ $ echo $PATH | zxc split : split / present print
17
+ ```
18
+ The command chain above will
19
+ 1. get the list of directories in your `$PATH`,
20
+ 2. split it into a list, using ":" as a delimiter,
21
+ 3. split each element of said list into a sublist, using "/" as a delimiter,
22
+ 4. remove empty elements,
23
+ 5. print every element of every sublist.
24
+
25
+ This works by generating a tree, where each node contains text, and exposing
26
+ "steps" to the user, which are procedures over said tree, In the example above,
27
+ "split", "present" and "print" are steps, the first one taking arguments.
28
+
29
+ Use `zxc -H` to read a manual listing available steps.
30
+ email:
31
+ - tiagomacedo@ufrj.br
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/zxc.rb
40
+ - lib/zxc/error.rb
41
+ - lib/zxc/errors/unknown_step_error.rb
42
+ - lib/zxc/errors/wrong_argument_number_error.rb
43
+ - lib/zxc/node.rb
44
+ - lib/zxc/step.rb
45
+ - lib/zxc/steps/first.rb
46
+ - lib/zxc/steps/last.rb
47
+ - lib/zxc/steps/match.rb
48
+ - lib/zxc/steps/match_regexp.rb
49
+ - lib/zxc/steps/present.rb
50
+ - lib/zxc/steps/print.rb
51
+ - lib/zxc/steps/print_tree.rb
52
+ - lib/zxc/steps/split.rb
53
+ - lib/zxc/steps/split_regexp.rb
54
+ - lib/zxc/version.rb
55
+ - man/zxc.1
56
+ - sig/zxc.rbs
57
+ homepage: https://github.com/tiago-macedo/zxc
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ homepage_uri: https://github.com/tiago-macedo/zxc
62
+ source_code_uri: https://github.com/tiago-macedo/zxc
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.0.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.3.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A little ruby program to quickly manipulate STDIN on the command line
82
+ test_files: []