Class: Bookmaker::Parser::Base
- Inherits:
-
Object
- Object
- Bookmaker::Parser::Base
- Defined in:
- lib/bookmaker/parser.rb
Overview
autoload :Txt , “bookmaker/parser/txt”
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
-
#config ⇒ Object
Return the configuration file.
- #entries ⇒ Object
-
#initialize(root_dir) ⇒ Base
constructor
A new instance of Base.
-
#name ⇒ Object
Return directory’s basename.
- #read_content(file) ⇒ Object
- #render_template(file, locals = {}) ⇒ Object
- #spawn_command(cmd) ⇒ Object
Constructor Details
#initialize(root_dir) ⇒ Base
24 25 26 27 |
# File 'lib/bookmaker/parser.rb', line 24 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.
14 15 16 |
# File 'lib/bookmaker/parser.rb', line 14 def root_dir @root_dir end |
#source ⇒ Object
Where the text files are stored.
18 19 20 |
# File 'lib/bookmaker/parser.rb', line 18 def source @source end |
Class Method Details
.parse(root_dir) ⇒ Object
20 21 22 |
# File 'lib/bookmaker/parser.rb', line 20 def self.parse(root_dir) new(root_dir).parse end |
Instance Method Details
#config ⇒ Object
Return the configuration file.
37 38 39 |
# File 'lib/bookmaker/parser.rb', line 37 def config Bookmaker.config(root_dir) end |
#entries ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bookmaker/parser.rb', line 40 def entries return @entries unless @entries.nil? files = Dir["text/**/*.tex"] @entries = {} files.each do |f| k = File.dirname(f) k.gsub!('text/','') @entries[k] = [] if @entries[k].nil? @entries[k] << f end return @entries end |
#name ⇒ Object
Return directory’s basename.
31 32 33 |
# File 'lib/bookmaker/parser.rb', line 31 def name File.basename(root_dir) end |
#read_content(file) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bookmaker/parser.rb', line 55 def read_content(file) content = File.read(file) begin if content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m content = "\n#{$'}\n" data = YAML.load($1) end return [content, data] rescue => e puts "Error reading file #{File.join(base, name)}: #{e.}" rescue SyntaxError => e puts "YAML Exception reading #{File.join(base, name)}: #{e.}" end end |
#render_template(file, locals = {}) ⇒ Object
52 53 54 |
# File 'lib/bookmaker/parser.rb', line 52 def render_template(file, locals = {}) ERB.new(File.read(file)).result OpenStruct.new(locals).instance_eval{ binding } end |
#spawn_command(cmd) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bookmaker/parser.rb', line 69 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 |