Module: LegalMarkdown

Defined in:
lib/legal_markdown.rb,
lib/legal_markdown/version.rb

Constant Summary collapse

VERSION =
"0.4.8"

Class Method Summary collapse

Class Method Details

.parse(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
94
95
# File 'lib/legal_markdown.rb', line 10

def self.parse(*args)
  if args.size == 1 && args.first.class == Array
    args = args.first
  end

  config={}
  config[:input] = {}
  config[:output] = {}

  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: legal2md [commands] [input_file] [output_file]"
    opt.separator ""
    opt.separator "[input_file] can be a file or use \"-\" for STDIN"
    opt.separator "[output_file] can be a file or use \"-\" for STDOUT"
    opt.separator ""
    opt.separator "Specific Commands:"

    config[:output][:markdown] = false
    opt.on( '-m', '--to-markdown', 'Parse the Legal Markdown file and produce a Text document (md, rst, txt, etc.).' ) do
      config[:output][:markdown] = true
    end

    config[:output][:jason] = false
    opt.on( '-j', '--to-json', 'Parse the Legal Markdown file and produce a JSON document.' ) do
      config[:output][:jason] = true
    end

    config[:verbose] = false
    opt.on('--verbose', 'Debug legal_markdown. Only works with output options, not with headers switch.') do
      config[:verbose] = true
    end

    config[:headers] = false
    opt.on( '-d', '--headers', 'Make the YAML Frontmatter automatically.' ) do
      config[:headers] = true
    end

    if args.include? :headers
      config[:headers] = true
      args.delete :headers
    end

    if args.include?( :to_json ) || (begin args[-1][/\.json/]; rescue; end;)
      config[:output][:jason] = true
      args.delete(:to_json) if args.include?( :to_json )
    end

    if args.include? :to_markdown || (begin args[-1][/\.md|\.markdown/]; rescue; end;)
      config[:output][:markdown] = true
      args.delete :markdown
    end

    opt.on( '-v', '--version', 'Display the gem\'s current version that you are using.' ) do
      puts 'Legal Markdown version ' + LegalMarkdown::VERSION
      exit
    end

    opt.on( '-h', '--help', 'Display this screen at any time.' ) do
      puts opt_parser
      exit
    end

    opt.separator ""
    opt.separator "Notes:"
    opt.separator "If the command is --headers or with --to-markdown you can enter one file to be parsed if you wish."
    opt.separator "When these commands are called with only one file I will set the input_file and the output_file to be the same."
    opt.separator "The other commands will require the original legal_markdown file and the output file."
    opt.separator "There is no need to explicitly enter the --to-json if your output_file is *.json I can handle it."
    opt.separator "There is no need to explicitly enter the --to-markdown if your output_file is *.md or *.markdown I can handle it."
    opt.separator ""
  end

  opt_parser.parse!(args)

  if args.size >= 1
    if config[:headers]
      MakeYamlFrontMatter.new(args)
    elsif config[:output][:jason]
      LegalToMarkdown.parse_jason(args, config[:verbose])
    elsif config[:output][:markdown] || args.size <= 2
      LegalToMarkdown.parse_markdown(args, config[:verbose])
    end
  else
    puts opt_parser
  end
end