Module: Murdoc

Defined in:
lib/murdoc/languages/html.rb,
lib/murdoc.rb,
lib/murdoc/scanner.rb,
lib/murdoc/version.rb,
lib/murdoc/renderer.rb,
lib/murdoc/annotator.rb,
lib/murdoc/paragraph.rb,
lib/murdoc/languages/base.rb,
lib/murdoc/languages/ruby.rb,
lib/murdoc/languages/markdown.rb,
lib/murdoc/formatted_paragraph.rb,
lib/murdoc/languages/javascript.rb,
lib/murdoc/languages/coffeescript.rb

Overview

Html language module

Defined Under Namespace

Modules: Languages Classes: AnnotatedFile, Annotator, FormattedParagraph, Paragraph, Renderer, Scanner

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.annotate(filename, highlight = true, do_not_count_comment_lines = false) ⇒ Object

‘Murdoc.annotate` arguments are gathered from CLI utility

‘highlight` regulates syntax highlighting and `do_not_count_comment_lines` flag toggles counting comment lines towards line numbering in the output.



22
23
24
25
26
27
28
29
30
31
# File 'lib/murdoc.rb', line 22

def self.annotate(filename, highlight = true, do_not_count_comment_lines = false)
  filename = File.expand_path(filename)
  annotator = Annotator.from_file(filename, nil, do_not_count_comment_lines)
  AnnotatedFile.new(filename,
      annotator.,
      annotator.source,
      annotator.source_type,
      annotator.paragraphs,
      annotator.paragraphs.map {|p| FormattedParagraph.new(p, highlight) })
end

.default_optionsObject

Rest is self-explanatory



117
118
119
120
121
122
123
# File 'lib/murdoc.rb', line 117

def self.default_options
  @options ||= {
    template:   "#{markup_dir}/template.haml",
    stylesheet: "#{markup_dir}/stylesheet.css",
    highlight: true
  }
end

.default_options_for_indexObject



144
145
146
147
148
149
# File 'lib/murdoc.rb', line 144

def self.default_options_for_index
  @options_index ||= {
    template: "#{markup_dir}/template_index.haml",
    stylesheet: "#{markup_dir}/stylesheet.css"
  }
end

.default_options_for_multiple_filesObject



125
126
127
128
129
130
131
# File 'lib/murdoc.rb', line 125

def self.default_options_for_multiple_files
  @options_multi ||= {
    template:   "#{markup_dir}/template_multifile.haml",
    stylesheet: "#{markup_dir}/stylesheet.css",
    highlight: true
  }
end

.default_options_for_treeObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/murdoc.rb', line 133

def self.default_options_for_tree
  @options_tree ||= {
    index_template: default_options_for_index[:template],
    index_stylesheet: default_options_for_index[:stylesheet],
    template: default_options[:template],
    stylesheet: default_options[:stylesheet],
    highlight: default_options[:highlight],
    do_not_count_comment_lines: false
  }
end

.generate_from_file(input, output, options = {}) ⇒ Object

Generate a single file story



34
35
36
37
38
39
40
41
42
# File 'lib/murdoc.rb', line 34

def self.generate_from_file(input, output, options = {})
  options = default_options.merge(options)
  annotator = Annotator.from_file(input, nil)
  File.open(output, "w+") do |f|
    annotated_file = annotate(input, options[:highlight], options[:do_not_count_comment_lines])
    f.puts Renderer.new(options[:template]).render(:annotated_file => annotated_file,
                                                   :stylesheet => File.read(options[:stylesheet]))
  end
end

.generate_from_multiple_files(input_files, output, options = {}) ⇒ Object

… or use multiple files



45
46
47
48
49
50
51
52
# File 'lib/murdoc.rb', line 45

def self.generate_from_multiple_files(input_files, output, options = {})
  options = default_options_for_multiple_files.merge(options)
  annotated_files = input_files.map {|fn| annotate(fn, options[:highlight], options[:do_not_count_comment_lines]) }
  File.open(output, "w+") do |f|
    f.puts Renderer.new(options[:template]).render(:annotated_files => annotated_files,
                                                   :stylesheet => File.read(options[:stylesheet]))
  end
end

.generate_index(fn, options) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/murdoc.rb', line 101

def self.generate_index(fn, options)
  options = default_options_for_index.merge(options)
  File.open(fn, 'w+') do |f|
    f.puts Renderer.new(options[:template]).render({
      stylesheet: File.read(options[:stylesheet]),
      base_dir: options[:base_dir],
      has_parent: options[:has_parent],
      current_directory: options[:current_directory],
      files: options[:files],
      directories: options[:directories]
    })
  end
end

.generate_tree(input_dir, output_dir, has_parent = false, base_dir = nil, options = {}) ⇒ Object

Generate a documentation tree



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
96
97
98
99
# File 'lib/murdoc.rb', line 55

def self.generate_tree(input_dir, output_dir, has_parent = false, base_dir = nil, options = {})
  options = default_options_for_tree.merge(options)
  input_dir = Pathname(input_dir).expand_path
  output_dir = Pathname(output_dir).expand_path
  base_dir ||= input_dir

  FileUtils.mkdir_p(output_dir)

  entries = input_dir.children

  directories = entries.select(&:directory?).
                        reject {|dir| dir == output_dir }.
                        reject {|dir| dir.basename.to_s.start_with?('.')}
  directories.each do |dir|
    next if dir == output_dir
    generate_tree(dir,
                  output_dir + dir.relative_path_from(input_dir),
                  true,
                  base_dir,
                  options)
  end

  files = entries.select(&:file?).select {|file| Languages.detect(file.to_s) }
  files.each do |file|
    relpath = file.relative_path_from(input_dir)
    generate_from_file(file, "#{output_dir}/#{relpath}.html", {
      highlight: options[:highlight],
      template: options[:template],
      stylesheet: options[:stylesheet],
      do_not_count_comment_lines: options[:do_not_count_comment_lines]
    })
  end

  unless files.empty? && directories.empty?
    generate_index("#{output_dir}/index.html", {
     current_directory: input_dir,
     files: files,
     directories: directories,
     has_parent: has_parent,
     base_dir: base_dir,
     template: options[:index_template],
     stylesheet: options[:index_stylesheet]
    })
  end
end

.markup_dirObject



151
152
153
# File 'lib/murdoc.rb', line 151

def self.markup_dir
  File.expand_path("../..", __FILE__)+ "/markup"
end

.try_load_yaml(yaml) ⇒ Object



156
157
158
159
160
# File 'lib/murdoc.rb', line 156

def self.try_load_yaml(yaml)
  YAML.load(yaml)
rescue => e
  nil
end