breakdown 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Breakdown
2
2
 
3
- TODO: Write a gem description
3
+ Breaks large markdown text files into many smaller ones, based on markdown compatible markup commands.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,39 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Breakdown extends the function of markdown section breaks, providing simple controls for splitting large files.
22
+
23
+ Any valid markdown horizontal section may be used. The following are all valid breakdown commands:
24
+
25
+ * * * home
26
+ *** page-1
27
+ ***** page-2
28
+ - - - about
29
+ --------------------------------------- my_very_long_page_name
30
+
31
+ Breakdown will create a new markdown file for each valid horizontal rule it finds. The new file will contain all the text between the horizontal rule and the next rule it finds (or the end of the file). Each file is named using the supplied title, with .md appended to the title. A page called 'home' will be created as 'home.md'.
32
+
33
+ Any text at the start of a file that doesn't have a named section will be placed in a file 'index.md'.
34
+
35
+ ### Page numbering
36
+
37
+ Pages in a sequence can be automatically numbered using the `:autonum` directive, e.g.
38
+
39
+ *** :autonum section
40
+ My first section
41
+ *** :autonum section
42
+ My second section
43
+
44
+ A markdown file like this will be broken into two smaller files, one called section-1.md and the other section-2.md
45
+
46
+ ### Discarding pages
47
+
48
+ Any page section starting with the `:discard` directive will be skipped during the breakdown process, e.g.
49
+
50
+ *** first_page
51
+ This page will created
52
+ *** :discard
53
+ This page will not
22
54
 
23
55
  ## Contributing
24
56
 
@@ -11,33 +11,90 @@ module Breakdown
11
11
  # ***** section :count # There will be a number of 'section' pages, with an id of 'section-1', 'section-2', etc
12
12
  # - - - # All horizontal rules can be used, per http://daringfireball.net/projects/markdown/syntax#hr
13
13
  # --------------------------------------- # And '#' '//' comments are comments
14
+
15
+ # NOTE One idea would be to 'preprocess' the file using grep, e.g.
16
+ # Kernal::system %{grep -n '^(^\* \* \*|\*\*\*)' filename} do |ok, res|
17
+ # if ! ok
18
+ # puts "pattern not found (status = #{res.exitstatus})"
19
+ # else
20
+ # # Do some preprocessing here... split, build up strategy, etc
21
+ # end
22
+ # end
23
+
24
+ # NOTE Naive first cut
25
+
26
+ # Valid markers:
27
+ # * * *
28
+ # ***
29
+ # *****
30
+ # - - -
31
+ # ---------------------------------------
14
32
 
15
- def self.process(filename, output_dir, options={})
16
-
17
- # NOTE One idea would be to 'preprocess' the file using grep, e.g.
18
- # Kernal::system %{grep -n '^(^\* \* \*|\*\*\*)' filename} do |ok, res|
19
- # if ! ok
20
- # puts "pattern not found (status = #{res.exitstatus})"
21
- # else
22
- # # Do some preprocessing here... split, build up strategy, etc
23
- # end
24
- # end
25
-
26
- # NOTE Naive first cut
33
+ class Processor
34
+
35
+ def initialize
36
+ @autonum_count = {}
37
+ end
38
+
39
+ def process(filename, output_dir, options={})
40
+
41
+ # Preconditions
42
+ raise "No such file: #{filename}" unless File.exist?(filename)
43
+ raise "Can't read file: #{filename}" unless File.readable?(filename)
44
+
45
+ # SMELL output_dir is used in two different variables
46
+ options = {:extension => 'md', :output_dir => output_dir}.merge(options)
47
+
48
+ FileUtils.mkdir_p output_dir # Ensure we have somewhere to put our files
49
+
50
+ f = File.open(filename)
51
+ begin
52
+ each_section(f) do |title, text|
53
+
54
+ command_match = title.match(/[:](?<command>.[a-z]*)(\s(?<params>.*)|$)/)
55
+ if command_match
56
+ self.send(command_match[:command], command_match[:params], text, options)
57
+ else
58
+ write_section(title, text, options)
59
+ end
60
+ end
61
+ #rescue
62
+ f.close # SMELL silent fail
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ # DSL methods
69
+
70
+ def autonum(*args)
71
+ title = args[0]
72
+ text = args[1]
73
+
74
+ options = args[2]
75
+
76
+ output_dir = options[:output_dir]
77
+
78
+ @autonum_sections = @autonum_sections || {}
79
+ @autonum_sections[title] = @autonum_sections[title] ? @autonum_sections[title] += 1 : 1
80
+
81
+ title = "#{title}-#{@autonum_sections[title]}"
82
+ write_section(title, text, options)
83
+ end
27
84
 
28
- # Valid markers:
29
- # * * *
30
- # ***
31
- # *****
32
- # - - -
33
- # ---------------------------------------
34
- def self.each_section(file)
85
+ def discard(*args)
86
+ # Do nothing
87
+ end
88
+
89
+ # Utility methods
90
+
91
+ def each_section(file)
35
92
  title = 'index'
36
93
  text = ''
37
-
94
+
38
95
  while line = file.gets do
39
96
  marker = line.match(/((^([*]{3}\s)|^([*]\s){3})|^([*]{5}\s)|^([-]\s){3}|^([-]{39}))(?<title>.*)/)
40
- #marker = line.match(/^\*\*\* (?<title>.*)/)
97
+
41
98
  if marker
42
99
  yield title, text if !text.empty?
43
100
  # start next section
@@ -48,28 +105,16 @@ module Breakdown
48
105
  end
49
106
  end
50
107
  yield title, text if !text.empty?
51
-
52
- end
53
-
54
- raise "No such file: #{filename}" unless File.exist?(filename)
55
- raise "Can't read file: #{filename}" unless File.readable?(filename)
56
-
57
- options = {:extension => 'md'}.merge(options)
58
-
59
- FileUtils.mkdir_p output_dir # Ensure we have somewhere to put our files
60
-
61
- f = File.open(filename)
62
- begin
63
-
64
- each_section(f) do |title, text|
65
-
66
- output_filename = File.join(output_dir, "/#{title}.#{options[:extension]}")
67
- File.open(output_filename, 'w') { |file| file.write text }
68
- end
69
-
70
- # rescue
71
- f.close # SMELL silent fail
72
108
  end
73
109
 
74
- end
110
+ def write_section(title, text, options)
111
+ output_filename = File.join(options[:output_dir], "/#{title}.#{options[:extension]}")
112
+ File.open(output_filename, 'w') { |file| file.write text }
113
+ end
114
+ end
115
+
116
+ def self.process(filename, output_dir, options={})
117
+ processor = Processor.new
118
+ processor.process(filename, output_dir, options)
119
+ end
75
120
  end
@@ -1,3 +1,3 @@
1
1
  module Breakdown
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,12 @@
1
+ require './spec/spec_helper'
2
+ require './lib/breakdown'
3
+
4
+ describe Breakdown do
5
+
6
+ it "should create numbered sections" do
7
+ filecount(@output_dir).should eq(0)
8
+ Breakdown::process './spec/examples/auto_section.md', @output_dir
9
+ filecount(@output_dir).should eq(3)
10
+ end
11
+
12
+ end
@@ -2,20 +2,9 @@ require './spec/spec_helper'
2
2
  require './lib/breakdown'
3
3
 
4
4
  describe Breakdown do
5
+
5
6
  describe "should break down text file" do
6
7
 
7
- def filecount(dir)
8
- Dir[File.join(dir, '/**/*')].count { |file| File.file?(file) }
9
- end
10
-
11
- before(:all) do
12
- @output_dir = "./spec/tmp/output/."
13
- end
14
-
15
- before(:each) do
16
- FileUtils.rm_rf(@output_dir, secure: true) # Clean out the output dir
17
- end
18
-
19
8
  it "on any ***" do
20
9
  filecount(@output_dir).should equal(0)
21
10
  Breakdown::process './spec/examples/star_star_star.md', @output_dir
@@ -46,4 +35,10 @@ describe Breakdown do
46
35
  filecount(@output_dir).should equal(2)
47
36
  end
48
37
  end
38
+
39
+ it "should not break on *** and a space" do
40
+ filecount(@output_dir).should equal(0)
41
+ Breakdown::process './spec/examples/three_stars_and_a_space.md', @output_dir
42
+ filecount(@output_dir).should equal(1)
43
+ end
49
44
  end
@@ -0,0 +1,12 @@
1
+ require './spec/spec_helper'
2
+ require './lib/breakdown'
3
+
4
+ describe Breakdown do
5
+
6
+ it "should not create discarded sections" do
7
+ filecount(@output_dir).should eq(0)
8
+ Breakdown::process './spec/examples/discard_section.md', @output_dir
9
+ filecount(@output_dir).should eq(2)
10
+ end
11
+
12
+ end
@@ -0,0 +1,6 @@
1
+ *** :autonum section
2
+ Foo
3
+ *** :autonum section
4
+ Bar
5
+ *** :autonum section
6
+ Bah
@@ -0,0 +1,6 @@
1
+ *** :autonum section
2
+ Foo
3
+ *** :discard
4
+ Bar
5
+ *** :autonum section
6
+ Bah
@@ -0,0 +1,3 @@
1
+ Foo
2
+ ***
3
+ Bar
@@ -7,4 +7,12 @@ RSpec.configure do |config|
7
7
 
8
8
  # Use the specified formatter
9
9
  config.formatter = :documentation # :progress, :html, :textmate
10
+
11
+ # Global before each/all blocks
12
+ config.before(:all) { @output_dir = "./spec/tmp/output/." }
13
+ config.before(:each) { FileUtils.rm_rf(@output_dir, secure: true) } # Clean out the output dir
14
+ end
15
+
16
+ def filecount(dir)
17
+ Dir[File.join(dir, '/**/*')].count { |file| File.file?(file) }
10
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breakdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-22 00:00:00.000000000 Z
12
+ date: 2013-05-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -74,7 +74,11 @@ files:
74
74
  - breakdown.gemspec
75
75
  - lib/breakdown.rb
76
76
  - lib/breakdown/version.rb
77
+ - spec/autonum_spec.rb
77
78
  - spec/breakdown_spec.rb
79
+ - spec/discard_spec.rb
80
+ - spec/examples/auto_section.md
81
+ - spec/examples/discard_section.md
78
82
  - spec/examples/example.md
79
83
  - spec/examples/five_stars.md
80
84
  - spec/examples/star_space_star_space_star_space.md
@@ -82,6 +86,7 @@ files:
82
86
  - spec/examples/the-tachypomp-and-other-stories.md
83
87
  - spec/examples/thirty_nine_dashes.md
84
88
  - spec/examples/three_dashes.md
89
+ - spec/examples/three_stars_and_a_space.md
85
90
  - spec/spec_helper.rb
86
91
  - spec/tachypomp_spec.rb
87
92
  homepage: ''
@@ -110,7 +115,11 @@ signing_key:
110
115
  specification_version: 3
111
116
  summary: Preprocessor for large text files
112
117
  test_files:
118
+ - spec/autonum_spec.rb
113
119
  - spec/breakdown_spec.rb
120
+ - spec/discard_spec.rb
121
+ - spec/examples/auto_section.md
122
+ - spec/examples/discard_section.md
114
123
  - spec/examples/example.md
115
124
  - spec/examples/five_stars.md
116
125
  - spec/examples/star_space_star_space_star_space.md
@@ -118,5 +127,6 @@ test_files:
118
127
  - spec/examples/the-tachypomp-and-other-stories.md
119
128
  - spec/examples/thirty_nine_dashes.md
120
129
  - spec/examples/three_dashes.md
130
+ - spec/examples/three_stars_and_a_space.md
121
131
  - spec/spec_helper.rb
122
132
  - spec/tachypomp_spec.rb