breakdown 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,6 +18,11 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ require 'breakdown'
22
+ Breakdown::process input_filename, output_dir
23
+
24
+ E.g. Breakdown::process _my_huge_markdown_file.md_, _contents_
25
+
21
26
  Breakdown extends the function of markdown section breaks, providing simple controls for splitting large files.
22
27
 
23
28
  Any valid markdown horizontal section may be used. The following are all valid breakdown commands:
@@ -51,6 +56,22 @@ Any page section starting with the `:discard` directive will be skipped during t
51
56
  This page will created
52
57
  *** :discard
53
58
  This page will not
59
+
60
+ ### Using blocks
61
+
62
+ Passing a block to the `Breakdown::process` method allows an opportunity to modify the title and text of each
63
+ section prior to writing out to a file.
64
+
65
+ Breakdown::process input_filename, output_dir do |section|
66
+ section[:title] = section[:title].upcase
67
+ section[:text] = convert_to_swedish_chef(section[:text])
68
+ section
69
+ end
70
+
71
+ Each `section` is hash containing values for `:title` and `:text` keys. It's important to pass the modified section
72
+ back (as the last statement in the block) so it can be accessed by the rest of the `process` method.
73
+
74
+ Typical uses for the block method is to add metadata to the head of a section's text, for example when using Breakdown to generate content for *nanoc* or *Jekyll*.
54
75
 
55
76
  ## Contributing
56
77
 
@@ -53,10 +53,14 @@ module Breakdown
53
53
 
54
54
  command_match = title.match(/[:](?<command>.[a-z]*)(\s(?<params>.*)|$)/)
55
55
  if command_match
56
- self.send(command_match[:command], command_match[:params], text, options)
56
+ section = self.send(command_match[:command], command_match[:params], text, options)
57
57
  else
58
- write_section(title, text, options)
59
- end
58
+ section = {:title => title, :text => text}
59
+ end
60
+
61
+ section = yield section if block_given?
62
+
63
+ write_section(section, options) unless section.nil?
60
64
  end
61
65
  #rescue
62
66
  f.close # SMELL silent fail
@@ -79,7 +83,8 @@ module Breakdown
79
83
  @autonum_sections[title] = @autonum_sections[title] ? @autonum_sections[title] += 1 : 1
80
84
 
81
85
  title = "#{title}-#{@autonum_sections[title]}"
82
- write_section(title, text, options)
86
+
87
+ {:title => title, :text => text}
83
88
  end
84
89
 
85
90
  def discard(*args)
@@ -89,9 +94,11 @@ module Breakdown
89
94
  # Utility methods
90
95
 
91
96
  def each_section(file)
92
- title = 'index'
97
+ title = 'index' # Default to 'index' if no title given
93
98
  text = ''
94
99
 
100
+ # TODO This could be more efficient by applying regular expressions to the whole file, finding
101
+ # breakpoints and processing them independently. The naive approach will do for now
95
102
  while line = file.gets do
96
103
  marker = line.match(/((^([*]{3}\s)|^([*]\s){3})|^([*]{5}\s)|^([-]\s){3}|^([-]{39}))(?<title>.*)/)
97
104
 
@@ -104,17 +111,17 @@ module Breakdown
104
111
  text += line
105
112
  end
106
113
  end
107
- yield title, text if !text.empty?
114
+ yield title, text if !text.empty? # Process any remaining text as a single section
108
115
  end
109
116
 
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 }
117
+ def write_section(section, options)
118
+ output_filename = File.join(options[:output_dir], "/#{section[:title]}.#{options[:extension]}")
119
+ File.open(output_filename, 'w') { |file| file.write section[:text] }
113
120
  end
114
121
  end
115
122
 
116
- def self.process(filename, output_dir, options={})
123
+ def self.process(filename, output_dir, options={}, &block)
117
124
  processor = Processor.new
118
- processor.process(filename, output_dir, options)
125
+ processor.process(filename, output_dir, options, &block)
119
126
  end
120
127
  end
@@ -1,3 +1,3 @@
1
1
  module Breakdown
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -21,6 +21,33 @@ describe Breakdown do
21
21
  Breakdown::process './spec/examples/the-tachypomp-and-other-stories.md', @output_dir
22
22
  filecount(@output_dir).should equal(9)
23
23
  end
24
+
25
+ it "should modify title and text when processed using a block" do
26
+
27
+ filecount(@output_dir).should equal(0)
28
+ Breakdown::process './spec/examples/the-tachypomp-and-other-stories.md', @output_dir do |section|
29
+ section[:title] = section[:title] + '-modified'
30
+ section[:text]= '!!modified!!' + section[:text]
31
+ section
32
+ end
33
+
34
+ %w(
35
+ index-modified.md
36
+ section-1-modified.md
37
+ section-2-modified.md
38
+ section-3-modified.md
39
+ section-4-modified.md
40
+ section-5-modified.md
41
+ section-6-modified.md
42
+ section-7-modified.md
43
+ table-of-contents-modified.md
44
+ ).each do |filename|
45
+ file_path = File.join @output_dir, filename
46
+ File.exist?(file_path).should be_true
47
+ text = File.read(file_path)
48
+ text.should include('!!modified!!')
49
+ end
50
+ end
24
51
  end
25
52
  end
26
53
 
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.2
4
+ version: 0.1.0
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-05-06 00:00:00.000000000 Z
12
+ date: 2013-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -130,3 +130,4 @@ test_files:
130
130
  - spec/examples/three_stars_and_a_space.md
131
131
  - spec/spec_helper.rb
132
132
  - spec/tachypomp_spec.rb
133
+ has_rdoc: