module Mdoc

individual processors

Extend link with ->https …

Delete extra blank lines inside ~~~~~ code bloks

rubocop:disable MethodLength

Constants

VERSION

Public Instance Methods

convert!(fname, doc_type = nil) { |pli| ... } click to toggle source

convert single file

# 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) click to toggle source

get a list of processors into a pipline

# 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) click to toggle source
# File lib/mdoc.rb, line 130
def default_processors(writer)
  writer.new.default_processors
end
execute!() click to toggle source

entry point of the application, for each source files read, process and write out to converted file

# 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) click to toggle source

from file name (esp. extensions), determine source file document type

# 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) click to toggle source

rubocop:enable MethodLength

# 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) click to toggle source

from several directory rubocop:disable MethodLength

# File lib/mdoc.rb, line 65
def find_tpl_file(fname)
  # add default search directory
  ipath = File.expand_path(File.dirname(__FILE__) + '/../templates')
  opts.tpl_directories << ipath unless opts.tpl_directories.include?(ipath)
  rpath = File.expand_path('./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) click to toggle source
# 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) click to toggle source

get class from keyword and module under Mdoc name space

# 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) click to toggle source
# 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) click to toggle source

load command line options rubocop:disable LineLength, MethodLength

# File lib/mdoc/options.rb, line 36
def load_cli_options(argv = ARGV)
  load_defaults unless opts
  argv = %w[-h] if argv.size == 0

  OptionParser.new do |opts|
    opts.banner = '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.expand_path(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) click to toggle source

load configuration files (if exists) from a list of candidates

# 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() click to toggle source

default configurations rubocop:disable MethodLength

# 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() click to toggle source

attr accessor for opts

# File lib/mdoc.rb, line 47
def opts
  load_defaults unless @opts
  @opts
end
prepare_doc(fname, doc_type) click to toggle source
# 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

Private Instance Methods

set_option!(hsh) click to toggle source

set options from a hash, raise errors if a key not exists in default hash

# File lib/mdoc/options.rb, line 100
def set_option!(hsh)
  hsh.each { |k, v| @opts.send(:"#{k}=", v) }
end