ntxt 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +8 -5
  2. data/bin/ntxt +95 -15
  3. data/lib/ntxt/block.rb +15 -3
  4. data/lib/ntxt/parser.rb +1 -3
  5. metadata +25 -41
@@ -7,7 +7,7 @@ to search their text in a slightly more structured way than +grep+ 'ing.
7
7
 
8
8
  Any tags found in a block are extracted and the block is _tagged_ with them.
9
9
  All parent blocks also receive the tags of their child blocks. Thus, the
10
- root block is tagged with all tags occuring in the document.
10
+ root block is tagged with all tags occurring in the document.
11
11
 
12
12
  == Format Rules
13
13
  === Headers
@@ -40,13 +40,15 @@ is bundled into a subblock to the 2-space indented block of text.
40
40
 
41
41
  The only ways to break out of this 2-indent text is to:
42
42
 
43
- 1. Put in an empty line.
44
- 2. Put in a header.
45
- 3. Indent more shallowly, such as a 1-space line.
43
+ 1. Put in an empty line. That is, a line with no indenting. A line with only
44
+ two spaces on it will be seen as a continuation of the 2-indent block.
45
+ 2. Indent more shallowly, such as a 1-space line.
46
+ 3. Put in a header.
46
47
 
47
48
  === Tags
48
49
 
49
- Lines beginning with [tag1] [tag2] are considered to have tags
50
+ Lines beginning with [tag1] [tag2] (ignoring whitespace) are considered to
51
+ have tags
50
52
  +tag1+ and +tag2+. For example:
51
53
 
52
54
  Block1
@@ -61,3 +63,4 @@ And finally, <code>not a tag</code> is, well, not a tag. It does not start a lin
61
63
  Also note that you can't tag header blocks directly because the header line
62
64
  must start with = and tag lines must begin with [. Blocks inherit all their
63
65
  child blocks' tags, though, so finding header blocks by tags is still possible.
66
+
data/bin/ntxt CHANGED
@@ -6,12 +6,19 @@ require 'ntxt'
6
6
  require 'yaml'
7
7
 
8
8
  $configs = {
9
- :cmd => 'tag',
9
+ :cmd => 'last',
10
+ :last => 1,
10
11
  :tag_string => '',
11
- :filename => "#{ENV['HOME']}/notes.txt"
12
+
13
+ # Replace with command of editor. Otherwise NTXT_EDITOR and EDITOR are used.
14
+ :editor => nil,
15
+
16
+ # Tag printing mode describes how tags are printed.
17
+ :tag_print_mode => 'path',
18
+ :filename => File.join(ENV['HOME'], "notes.txt")
12
19
  }
13
20
 
14
- DEFAULT_CONFIG_FILE="#{ENV['HOME']}/.ntxtrc"
21
+ DEFAULT_CONFIG_FILE=File.join(ENV['HOME'], ".ntxtrc")
15
22
 
16
23
  # Create or load a configuration file.
17
24
  if File.exists?(DEFAULT_CONFIG_FILE)
@@ -25,7 +32,7 @@ else
25
32
  end
26
33
 
27
34
  OptionParser.new do |opt|
28
- opt.version="1.0.4"
35
+ opt.version="1.0.5"
29
36
 
30
37
  opt.on('--trace', 'Trace through the file printing summaries.' ) do |v|
31
38
  $configs[:cmd] = 'trace'
@@ -43,13 +50,23 @@ OptionParser.new do |opt|
43
50
  $configs[:cmd] = 'print_tags'
44
51
  end
45
52
 
46
- opt.on('-t', '--tag=String', 'Search and print the give so-tagged blocks.') do |v|
53
+ opt.on('-t', '--tag=String', 'Print blocks with tags contaning this.') do |v|
47
54
  $configs[:cmd] = 'tag'
48
55
  $configs[:tag_string] = v
49
56
  end
50
57
 
58
+ opt.on('--path', 'Print the path down the tree.') do |v|
59
+ $configs[:tag_print_mode] = 'path'
60
+ end
61
+ opt.on('--parent', 'Print the whole parent block.') do |v|
62
+ $configs[:tag_print_mode] = 'parent'
63
+ end
64
+ opt.on('--leaf', 'Print only the tagged block.') do |v|
65
+ $configs[:tag_print_mode] = 'leaf'
66
+ end
67
+
51
68
  opt.on('-l', '--last=[Integer]',
52
- 'Show the last n top-level blocks. Default is 1') do |v|
69
+ 'Show the last n top-level blocks.') do |v|
53
70
  $configs[:cmd] = 'last'
54
71
  $configs[:last] = (v.nil?)? 1 : v.to_i
55
72
  end
@@ -72,6 +89,71 @@ def printNonEmpty(txt)
72
89
  end
73
90
  end
74
91
 
92
+ module TagPrintModes
93
+
94
+ def self.each_matched_tag(block, tag_string)
95
+ block.
96
+ tags.
97
+ keys.
98
+ # Filter the tags we want.
99
+ select { |x| x.index(tag_string) }.
100
+
101
+ # Process the tags we've selected.
102
+ each { |tag| yield tag }
103
+ end
104
+
105
+ # Only print the block if the tag count == 0.
106
+ def self.leaf(ntxt, tag_string)
107
+ ntxt.walkText(
108
+ # Processing a block
109
+ lambda { |txt, depth, block| },
110
+ lambda do |depth, block|
111
+ each_matched_tag(block, tag_string) do |tag|
112
+ printNonEmpty(block.text) if block.tags[tag] == 0
113
+ end
114
+ end,
115
+ lambda { |depth, block| } )
116
+ end
117
+
118
+ # Print the entire parent block if the tag count == 0
119
+ # and the parent tag count == 1. Otherwise, print txt.
120
+ def self.parent(ntxt, tag_string)
121
+ ntxt.walkText(
122
+ # Processing a block
123
+ lambda { |txt, depth, block| },
124
+ lambda do |depth, block|
125
+ each_matched_tag(block, tag_string) do |tag|
126
+ if block.tags[tag] == 0 and
127
+ block.parent and
128
+ block.parent.tags[tag] == 1
129
+ printNonEmpty(block.parent.text)
130
+ end
131
+ end
132
+ end,
133
+ lambda { |depth, block| } )
134
+ end
135
+
136
+ # Print txt always. Effectively this presents a pruned
137
+ # text path of text that is tagged.
138
+ #
139
+ # This is essentially the Ntxt::Ntxt.walkText algorithm.
140
+ def self.path(ntxt, tag_string)
141
+ ntxt.walkText(
142
+ # Processing a block
143
+ lambda do |txt, depth, block|
144
+ each_matched_tag(block, tag_string) do |tag|
145
+ printNonEmpty(txt) if block.tags[tag] > 0
146
+ end
147
+ end,
148
+ lambda do |depth, block|
149
+ each_matched_tag(block, tag_string) do |tag|
150
+ printNonEmpty(block.text) if block.tags[tag] == 0
151
+ end
152
+ end,
153
+ lambda { |depth, block| } )
154
+ end
155
+ end
156
+
75
157
  $configs[:tag_string] = ARGV.shift if ARGV.length > 0
76
158
 
77
159
  begin
@@ -86,15 +168,11 @@ when 'print_tags'
86
168
  # Notice that we are re-wrapping the tags back into square brackets.
87
169
  puts "[#{ntxt.rootBlock.tags.keys.sort.join('] [')}]"
88
170
  when 'tag'
89
- ntxt.walkText(
90
- lambda { |txt, depth, block|
91
- printNonEmpty txt if block.tags.keys.sort.join(', ').index( $configs[:tag_string])},
92
- lambda { |depth, block| },
93
- lambda { |depth, block| } )
171
+ TagPrintModes.send($configs[:tag_print_mode], ntxt, $configs[:tag_string])
94
172
  when 'search'
95
173
  ntxt.walkText(
96
174
  lambda { |txt, depth, block|
97
- printNonEmpty txt if txt && txt.index( $configs[:search_string])},
175
+ printNonEmpty txt if txt && txt.index($configs[:search_string])},
98
176
  lambda { |depth, block| },
99
177
  lambda { |depth, block| } )
100
178
  when 'print'
@@ -103,14 +181,16 @@ when 'print'
103
181
  lambda { |depth, block| },
104
182
  lambda { |depth, block| } )
105
183
  when 'last'
106
- ntxt.rootBlock.children[-$configs[:last].. -1].each do |blk|
184
+ ntxt.rootBlock.children[-$configs[:last] .. -1].each do |blk|
107
185
  puts blk.text
108
186
  end
109
187
  when 'edit'
110
- editor = ENV['NTXT_EDITOR'] || ENV['EDITOR']
188
+ editor = $configs[:editor] || ENV['NTXT_EDITOR'] || ENV['EDITOR']
111
189
 
112
190
  if editor.nil?
113
- puts "Environment variable NTXT_EDITOR and EDITOR are not defined."
191
+ puts "No editor defined."
192
+ puts "No editor entry in .ntxtrc."
193
+ puts "No editor entry in NTXT_EDITOR or EDITOR environment variables."
114
194
  else
115
195
  system("#{editor} \"#{$configs[:filename]}\"")
116
196
  end
@@ -60,9 +60,21 @@ module Ntxt
60
60
  end
61
61
 
62
62
  # Add a tag to this block and all ancestor blocks.
63
- def addTag(tag)
64
- @tags[tag] += 1
65
- @parent.addTag(tag) if @parent
63
+ #
64
+ # A tag added to a block does not increment that block's tag count
65
+ # for the added tag. A tag added to the parent of a tagged block has
66
+ # it's tag count incremented by 1.
67
+ #
68
+ # Thus, if a tag count = 0, then this block owns the tag.
69
+ #
70
+ # If a tag count = 1. there is 1 child block with the given tag.
71
+ #
72
+ # [tag] The tag to add.
73
+ # [inc] The increment value. This should always be 0
74
+ # for client code.
75
+ def addTag(tag, inc=0)
76
+ @tags[tag] += inc
77
+ @parent.addTag(tag, 1) if @parent
66
78
  end
67
79
 
68
80
  # Return the text slice that this block refers to.
@@ -206,9 +206,7 @@ module Ntxt
206
206
  # Check if we have discovered another block in the form of an hlevel.
207
207
  hl = Parser::hlevel(line)
208
208
 
209
- if hl && hl[0].to_i <= level
210
- break
211
- end
209
+ break if hl && hl[0] <= level
212
210
 
213
211
  line = state.nextLine
214
212
  end
metadata CHANGED
@@ -1,74 +1,58 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ntxt
3
- version: !ruby/object:Gem::Version
4
- hash: 31
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.5
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 4
10
- version: 1.0.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Sam Baskinger
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-12-09 00:00:00 Z
12
+ date: 2013-02-25 00:00:00.000000000 Z
19
13
  dependencies: []
14
+ description: ! 'A library and command line tool for parsing plain text into blocks
15
+ by
20
16
 
21
- description: |
22
- A library and command line tool for parsing plain text into blocks by
23
17
  indentation and a simple wiki-esque == header == notation. Tagging of blocks
18
+
24
19
  and searching them is also supported.
25
20
 
21
+ '
26
22
  email: basking2@yahoo.com
27
- executables:
23
+ executables:
28
24
  - ntxt
29
25
  extensions: []
30
-
31
26
  extra_rdoc_files: []
32
-
33
- files:
27
+ files:
34
28
  - README.rdoc
29
+ - lib/ntxt.rb
35
30
  - lib/ntxt/block.rb
36
- - lib/ntxt/parser.rb
37
31
  - lib/ntxt/ntxt.rb
38
- - lib/ntxt.rb
32
+ - lib/ntxt/parser.rb
39
33
  - bin/ntxt
40
34
  homepage: http://coffeesgone.wordpress.com
41
35
  licenses: []
42
-
43
36
  post_install_message:
44
37
  rdoc_options: []
45
-
46
- require_paths:
38
+ require_paths:
47
39
  - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
40
+ required_ruby_version: !ruby/object:Gem::Requirement
49
41
  none: false
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- hash: 3
54
- segments:
55
- - 0
56
- version: "0"
57
- required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
47
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- hash: 3
63
- segments:
64
- - 0
65
- version: "0"
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
66
52
  requirements: []
67
-
68
53
  rubyforge_project:
69
- rubygems_version: 1.7.2
54
+ rubygems_version: 1.8.25
70
55
  signing_key:
71
56
  specification_version: 3
72
57
  summary: A parser and some tools for the ntxt text block format.
73
58
  test_files: []
74
-