Class: Verku::Exporter::Base
- Inherits:
-
Object
- Object
- Verku::Exporter::Base
- Defined in:
- lib/verku/exporter/base.rb
Constant Summary collapse
- EXTENSIONS =
%w[markdown mkdown mkdn mkd md text]
Instance Attribute Summary collapse
-
#root_dir ⇒ Object
The e-book directory.
-
#source ⇒ Object
Where the text files are stored.
Class Method Summary collapse
Instance Method Summary collapse
- #base_name(ext = '') ⇒ Object
- #build_data ⇒ Object
-
#config ⇒ Object
Return the configuration file.
- #epub_file ⇒ Object
- #git_branch ⇒ Object
- #handle_error(error) ⇒ Object
- #html_file ⇒ Object
-
#initialize(root_dir) ⇒ Base
constructor
A new instance of Base.
-
#name ⇒ Object
Return directory’s basename.
- #output_name(ext = 'txt') ⇒ Object
- #read_content(file) ⇒ Object
- #render_template(file, locals = {}) ⇒ Object
- #source_list ⇒ Object
- #spawn_command(cmd) ⇒ Object
- #tex_file ⇒ Object
- #ui ⇒ Object
Constructor Details
#initialize(root_dir) ⇒ Base
Returns a new instance of Base.
21 22 23 24 |
# File 'lib/verku/exporter/base.rb', line 21 def initialize(root_dir) @root_dir = Pathname.new(root_dir) @source = root_dir.join("text") end |
Instance Attribute Details
#root_dir ⇒ Object
The e-book directory.
8 9 10 |
# File 'lib/verku/exporter/base.rb', line 8 def root_dir @root_dir end |
#source ⇒ Object
Where the text files are stored.
9 10 11 |
# File 'lib/verku/exporter/base.rb', line 9 def source @source end |
Class Method Details
.export!(root_dir) ⇒ Object
17 18 19 |
# File 'lib/verku/exporter/base.rb', line 17 def self.export!(root_dir) new(root_dir).export! end |
Instance Method Details
#base_name(ext = '') ⇒ Object
38 39 40 |
# File 'lib/verku/exporter/base.rb', line 38 def base_name(ext='') oname = "#{name}-#{git_branch}" end |
#build_data ⇒ Object
49 50 51 52 53 |
# File 'lib/verku/exporter/base.rb', line 49 def build_data source_list.each do |file| data = read_content(file)[1] end end |
#config ⇒ Object
Return the configuration file.
46 47 48 |
# File 'lib/verku/exporter/base.rb', line 46 def config Verku.config(root_dir) end |
#epub_file ⇒ Object
68 69 70 |
# File 'lib/verku/exporter/base.rb', line 68 def epub_file output_name("epub") end |
#git_branch ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/verku/exporter/base.rb', line 30 def git_branch branch = if (File.exist?(root_dir.join(".git/HEAD"))) File.read( root_dir.join(".git/HEAD") ).sub("ref: refs/heads/","").sub(/\n/,'') else "none" end return branch end |
#handle_error(error) ⇒ Object
13 14 15 16 |
# File 'lib/verku/exporter/base.rb', line 13 def handle_error(error) ui.say "#{error.class}: #{error.}", :red ui.say error.backtrace.join("\n"), :white end |
#html_file ⇒ Object
65 66 67 |
# File 'lib/verku/exporter/base.rb', line 65 def html_file output_name("html") end |
#name ⇒ Object
Return directory’s basename.
27 28 29 |
# File 'lib/verku/exporter/base.rb', line 27 def name File.basename(root_dir) end |
#output_name(ext = 'txt') ⇒ Object
41 42 43 |
# File 'lib/verku/exporter/base.rb', line 41 def output_name(ext='txt') root_dir.join("builds/#{base_name}.#{ext}") end |
#read_content(file) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/verku/exporter/base.rb', line 74 def read_content(file) content = File.read(file) data = {} begin # YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m content = $POSTMATCH data = YAML.load($1, :safe => true) end return [content, data] rescue SyntaxError => e puts "YAML Exception reading #{file}: #{e.}" rescue Exception => e puts "Error reading file #{file}: #{e.}" end end |
#render_template(file, locals = {}) ⇒ Object
59 60 61 |
# File 'lib/verku/exporter/base.rb', line 59 def render_template(file, locals = {}) ERB.new(File.read(file)).result OpenStruct.new(locals).instance_eval{ binding } end |
#source_list ⇒ Object
55 56 57 58 |
# File 'lib/verku/exporter/base.rb', line 55 def source_list # @source_list ||= SourceList.new(root_dir) @source_list ||= Dir.glob("#{@root_dir.join('text')}/**/*.{#{EXTENSIONS.join(",")}}") end |
#spawn_command(cmd) ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/verku/exporter/base.rb', line 90 def spawn_command(cmd) begin stdout_and_stderr, status = Open3.capture2e(*cmd) rescue Errno::ENOENT => e puts e. else puts stdout_and_stderr unless status.success? status.success? end end |
#tex_file ⇒ Object
71 72 73 |
# File 'lib/verku/exporter/base.rb', line 71 def tex_file output_name("tex") end |
#ui ⇒ Object
62 63 64 |
# File 'lib/verku/exporter/base.rb', line 62 def ui @ui ||= Thor::Base.shell.new end |