Module: Mdoc
- Extended by:
- Mdoc
- Included in:
- Mdoc
- Defined in:
- lib/mdoc.rb,
lib/mdoc/meta.rb,
lib/mdoc/writer.rb,
lib/mdoc/options.rb,
lib/mdoc/version.rb,
lib/mdoc/document.rb,
lib/mdoc/pipeline.rb,
lib/mdoc/processor.rb,
lib/mdoc/processor/jqplot.rb,
lib/mdoc/document/kramdown.rb,
lib/mdoc/processor/add_toc.rb,
lib/mdoc/processor/add_title.rb,
lib/mdoc/writer/pandoc_writer.rb,
lib/mdoc/processor/expand_link.rb,
lib/mdoc/processor/js_sequence.rb,
lib/mdoc/processor/smart_code_block.rb
Overview
Delete extra blank lines inside ~~~~~ code bloks
rubocop:disable MethodLength
Defined Under Namespace
Classes: Document, Meta, PandocWriter, Pipeline, Processor, Writer
Constant Summary collapse
- VERSION =
'0.0.11'
Instance Method Summary collapse
-
#convert!(fname, doc_type = nil) {|pli| ... } ⇒ Object
convert single file.
-
#default_pipeline(doc, writer) ⇒ Object
get a list of processors into a pipline.
- #default_processors(writer) ⇒ Object
-
#execute! ⇒ Object
entry point of the application, for each source files read, process and write out to converted file.
-
#find_doc_type(f) ⇒ Object
from file name (esp. extensions), determine source file document type.
-
#find_out_file(fname) ⇒ Object
rubocop:enable MethodLength.
-
#find_tpl_file(fname) ⇒ Object
from several directory rubocop:disable MethodLength.
- #find_writer(doc) ⇒ Object
-
#get_class(cname, mdl = Processor) ⇒ Object
get class from keyword and module under Mdoc name space.
- #get_processor(pn) ⇒ Object
-
#load_cli_options(argv = ARGV) ⇒ Object
load command line options rubocop:disable LineLength, MethodLength.
-
#load_conf_files(file_ary) ⇒ Object
load configuration files (if exists) from a list of candidates.
-
#load_defaults ⇒ Object
default configurations rubocop:disable MethodLength.
-
#opts ⇒ Object
attr accessor for opts.
- #prepare_doc(fname, doc_type) ⇒ Object
Instance Method Details
#convert!(fname, doc_type = nil) {|pli| ... } ⇒ Object
convert single file
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mdoc.rb', line 33 def convert!(fname, doc_type = nil) doc = prepare_doc(fname, doc_type) # apply pipeline of processors # TODO: separate writer writer = find_writer(doc) pli = default_pipeline(doc, writer) yield pli if block_given? # receive user supplied processors pli.writer = writer unless pli.writer pli.apply!(doc) doc # return doc end |
#default_pipeline(doc, writer) ⇒ Object
get a list of processors into a pipline
123 124 125 126 127 128 |
# File 'lib/mdoc.rb', line 123 def default_pipeline(doc, writer) pli = Pipeline.new default_processors(writer) opts.processors.each { |p| pli.append p } opts.no_processors.each { |p| pli.remove p } pli end |
#default_processors(writer) ⇒ Object
130 131 132 |
# File 'lib/mdoc.rb', line 130 def default_processors(writer) writer.new.default_processors end |
#execute! ⇒ Object
entry point of the application, for each source files read, process and write out to converted file
24 25 26 27 28 29 30 |
# File 'lib/mdoc.rb', line 24 def execute! load_defaults unless @opts opts.s_files.each do |sname| Dir[sname].each { |fname| convert!(fname) } end end |
#find_doc_type(f) ⇒ Object
from file name (esp. extensions), determine source file document type
114 115 116 117 118 119 120 |
# File 'lib/mdoc.rb', line 114 def find_doc_type(f) case f when /\.(md|markdown)/ Document::Kramdown else Document end end |
#find_out_file(fname) ⇒ Object
rubocop:enable MethodLength
91 92 93 94 95 |
# File 'lib/mdoc.rb', line 91 def find_out_file(fname) return opts.output if opts.output ext = '.' + opts.template.split('.')[-1] fname =~ /\.\w{2,5}$/ ? fname.gsub(/\.\w+$/, ext) : fname + ext end |
#find_tpl_file(fname) ⇒ Object
from several directory rubocop:disable MethodLength
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mdoc.rb', line 65 def find_tpl_file(fname) # add default search directory ipath = File.(File.dirname(__FILE__) + '/../templates') opts.tpl_directories << ipath unless opts.tpl_directories.include?(ipath) rpath = File.('./templates') opts.tpl_directories << rpath unless opts.tpl_directories.include?(rpath) fname = 'default.' + fname unless fname =~ /\./ cand, buf = [], nil fname.split('.').reverse.each do |b| buf = buf ? b + '.' + buf : b cand.unshift buf end cand.each do |pt| opts.tpl_directories.each do |d| tpl = d + '/' + pt + '.erb' return tpl if File.exists?(tpl) end end # raise 'no template file found for ' + opts.template nil end |
#find_writer(doc) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/mdoc.rb', line 134 def find_writer(doc) case opts.template when /pandoc\.\w+$/ PandocWriter when /(epub|docx)/ # no native support PandocWriter else Writer end end |
#get_class(cname, mdl = Processor) ⇒ Object
get class from keyword and module under Mdoc name space
98 99 100 101 102 |
# File 'lib/mdoc.rb', line 98 def get_class(cname, mdl = Processor) mdl.const_get(cname.split(/[\,\_]/).map { |p| p.capitalize }.join) rescue NameError return nil end |
#get_processor(pn) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/mdoc.rb', line 104 def get_processor(pn) pname = pn.is_a?(String) ? pn : pn.class pn = get_class(pn) if pn.is_a?(String) # for string name raise "not a valid class: #{pname.to_s}" unless pn raise "not a processor: #{pname.to_s}" unless pn < Mdoc::Processor pn end |
#load_cli_options(argv = ARGV) ⇒ Object
load command line options rubocop:disable LineLength, MethodLength
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mdoc/options.rb', line 36 def (argv = ARGV) load_defaults unless opts argv = %w[-h] if argv.size == 0 OptionParser.new do |opts| opts. = 'Usage: mdoc [options] file.md [file2.md]' opts.separator '' opts.separator 'Options: ' opts.on('-t TPL', '--template TPL', 'output file template') do |tpl| set_option!({ template: tpl }) end opts.on('-o FILE', '--output FILE', 'output file template') do |file| set_option!({ output: file }) end opts.on('-p P,P2', '--processors P,P2', 'enable processors') do |p| p.split(',').each do |pr| @opts.processors << pr unless @opts.processors.include?(pr) end end opts.on('-z P,P2', '--disable P,P2', 'disable processors') do |op| op.split(',').each do |pr| @opts.no_processors << pr unless @opts.processors.include?(pr) end end opts.on('-d D,D2', '--template_directories D,D2', 'directories for finding template') do |d| d.split(',').each do |dir| dir = File.(dir) @opts.tpl_directories << dir unless @opts.tpl_directories.include?(dir) end end opts.on('-O', '--no-output', 'dump result to STDOUT') do @opts.no_output = true end opts.on_tail('-v', '--version', 'show mdoc version') do puts Mdoc::VERSION exit end opts.on_tail('-h', '--help', 'display this screen') do puts opts exit end end.parse!(argv) set_option!({ s_files: argv }) # check consistency for related options raise 'you can not specify output file when there are more than on source files.' if opts.output && opts.s_files.size > 0 raise 'you can not speficy output file with --no-output option' if opts.output && opts.no_output end |
#load_conf_files(file_ary) ⇒ Object
load configuration files (if exists) from a list of candidates
25 26 27 28 29 30 31 32 |
# File 'lib/mdoc/options.rb', line 25 def load_conf_files(file_ary) load_defaults unless opts file_ary.each do |file| yml = YAML.load(File.open(file, 'r:utf-8').read) if File.exists? file set_option! yml if yml end end |
#load_defaults ⇒ Object
default configurations rubocop:disable MethodLength
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mdoc/options.rb', line 7 def load_defaults hsh = { template: 'html', output: nil, no_output: false, processors: [], no_processors: [], tpl_directories: [], s_files: [], } # create a dynamic struct with default values @opts = Struct.new(*hsh.keys).new(*hsh.values) end |
#opts ⇒ Object
attr accessor for opts
47 48 49 50 |
# File 'lib/mdoc.rb', line 47 def opts load_defaults unless @opts @opts end |
#prepare_doc(fname, doc_type) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mdoc.rb', line 52 def prepare_doc(fname, doc_type) doc_type = find_doc_type(fname) unless doc_type doc = doc_type.new(fname) # template doc.tpl_file = find_tpl_file(opts.template) # output file doc.out_file = find_out_file(fname) unless opts.no_output doc end |