asciidoc-merger 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'http://rubygems.org'
2
+ gemspec
@@ -0,0 +1,3 @@
1
+ require_relative "./asciidoc-merger/parser"
2
+ require_relative "./asciidoc-merger/path_mapping"
3
+ require_relative "./asciidoc-merger/version"
@@ -0,0 +1,158 @@
1
+ require 'fileutils'
2
+ require 'pathname'
3
+
4
+ module AsciiDocMerger
5
+
6
+ STANDARD_IMAGE_PATH = "assets"
7
+ DEFAULT_MERGED_FILE_NAME = "merged_document.asciidoc"
8
+
9
+ class Parser
10
+
11
+ attr_accessor :output, :file, :image_folder, :pwd, :ignore_non_existing_images, :output_folder, :merged_file_name
12
+
13
+ def initialize
14
+ @pwd = Dir.pwd
15
+ end
16
+
17
+ def merge!
18
+ parse_main_file!
19
+ save!
20
+ end
21
+
22
+ private
23
+
24
+ def parse_main_file!
25
+ @output = parse_includes @file
26
+ end
27
+
28
+ def parse_includes(current_file)
29
+ unprocessed_output = read(current_file)
30
+ includes = find_include_paths(unprocessed_output)
31
+
32
+ processed_output = unprocessed_output
33
+ processed_output = parse_images! processed_output, File.dirname(current_file)
34
+
35
+ for mapping in includes
36
+ processed_output = process_output(processed_output, mapping)
37
+ end
38
+ processed_output
39
+ end
40
+
41
+ def process_output(unprocessed_output, file_mapping)
42
+ sub_output = parse_includes file_mapping.file
43
+ unprocessed_output.gsub(file_mapping.text, sub_output)
44
+ end
45
+
46
+ def find_include_paths(input)
47
+ cleanup_paths input.to_enum(:scan, /include::(.*)\[(.*)\]/).map { Regexp.last_match }
48
+ end
49
+
50
+ def parse_images!(input, current_file_path)
51
+ image_list = find_images input
52
+ if image_list.any?
53
+ copy_images image_list, current_file_path
54
+ output = replace_image_paths image_list, input
55
+ else
56
+ output = input
57
+ end
58
+ output
59
+ end
60
+
61
+ def replace_image_paths(images, input)
62
+
63
+ output = input
64
+
65
+ images.each do |match|
66
+ new_string = match[0]
67
+ basename = File.basename match[2]
68
+ new_string = new_string.gsub(match[2], File.join(relative_assets_path, basename))
69
+
70
+ output = output.gsub(match[0], new_string)
71
+
72
+ end
73
+
74
+ output
75
+ end
76
+
77
+ def find_images(input)
78
+ # inline images
79
+ images = input.to_enum(:scan, /image:(:)?(.*)\[(.*)\]/).map { Regexp.last_match }
80
+ images
81
+ end
82
+
83
+ def copy_images(remote_files, current_file_path)
84
+ if !@image_folder
85
+ @image_folder = AsciiDocMerger::STANDARD_IMAGE_PATH
86
+ end
87
+ FileUtils.mkdir_p(@image_folder)
88
+ if remote_files.any?
89
+ remote_files.each do |file|
90
+ basename = File.basename file[2]
91
+ Dir.chdir(File.dirname(@file)) do
92
+ original_file_path = File.join(current_file_path, file[2])
93
+ destination_path = File.join(assets_path, basename)
94
+ copy_file! original_file_path, destination_path, current_file_path
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ def assets_path
101
+ File.join(save_path, @image_folder)
102
+ end
103
+
104
+ def relative_assets_path
105
+ Pathname.new(assets_path).relative_path_from(Pathname.new(save_path)).to_s
106
+ end
107
+
108
+ def save_path
109
+ if @output_folder
110
+ @output_folder
111
+ else
112
+ @pwd
113
+ end
114
+ end
115
+
116
+ def copy_file!(source, destination, current_file_path)
117
+ basename = File.basename source
118
+ if File.exists? source
119
+ FileUtils.cp( source, destination)
120
+ else
121
+ if !@ignore_non_existing_images
122
+ raise IOError, "Could not find file: #{basename} in file: #{current_file_path}"
123
+ end
124
+ end
125
+ end
126
+
127
+ def cleanup_paths(includes)
128
+ paths = []
129
+ for match in includes
130
+ paths.push PathMapping.new(match[0], match[1])
131
+ end
132
+ paths
133
+ end
134
+
135
+ def read(relative_file_path)
136
+ Dir.chdir(File.dirname(@file)) do
137
+ File.read(File.expand_path(relative_file_path))
138
+ end
139
+ end
140
+
141
+ def get_file_name
142
+ if @merged_file_name
143
+ @merged_file_name
144
+ else
145
+ DEFAULT_MERGED_FILE_NAME
146
+ end
147
+ end
148
+
149
+ def save!
150
+ file = File.join(save_path, get_file_name)
151
+ merged_document = File.open( file, 'w' )
152
+ merged_document << @output
153
+ merged_document.close
154
+ end
155
+
156
+ end
157
+
158
+ end
@@ -0,0 +1,11 @@
1
+ module AsciiDocMerger
2
+
3
+ class PathMapping
4
+ attr_accessor :text, :file
5
+ def initialize(text, file)
6
+ @text = text
7
+ @file = file
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module AsciiDocMerger
2
+ VERSION = '0.2'
3
+ end
@@ -0,0 +1,5 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe AsciiDocMerger::Base do
4
+
5
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe AsciiDocMerger do
4
+
5
+ it "must be defined" do
6
+ AsciiDocMerger::VERSION.wont_be_nil
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/asciidoc-merger.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asciidoc-merger
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vince Verberckt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Merge different asciidoc files into 1 big file.
31
+ email:
32
+ - vince@verberckt.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - Gemfile
38
+ - lib/asciidoc-merger/parser.rb
39
+ - lib/asciidoc-merger/version.rb
40
+ - lib/asciidoc-merger/path_mapping.rb
41
+ - lib/asciidoc-merger.rb
42
+ - test/test_helper.rb
43
+ - test/lib/asciidoc-merger/version_test.rb
44
+ - test/lib/asciidoc-merger/base_test.rb
45
+ homepage: http://github.com
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.25
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Merge different asciidoc files into 1 big file.
69
+ test_files:
70
+ - test/test_helper.rb
71
+ - test/lib/asciidoc-merger/version_test.rb
72
+ - test/lib/asciidoc-merger/base_test.rb