Class: Cdoc::DocString
Instance Method Summary collapse
- #content ⇒ Object
- #finish ⇒ Object
-
#initialize ⇒ DocString
constructor
A new instance of DocString.
- #render_as_html ⇒ Object
- #section(str) ⇒ Object
- #sidebar(keys) ⇒ Object
- #subsection(str) ⇒ Object
- #tagged_line(line, tag) ⇒ Object
- #title(title) ⇒ Object
- #unordered_list?(line) ⇒ Boolean
Methods included from Helpers
Constructor Details
#initialize ⇒ DocString
Returns a new instance of DocString.
22 23 24 |
# File 'lib/cdoc.rb', line 22 def initialize @content = [] end |
Instance Method Details
#content ⇒ Object
155 156 157 |
# File 'lib/cdoc.rb', line 155 def content @content.flatten.join('') end |
#finish ⇒ Object
150 151 152 153 |
# File 'lib/cdoc.rb', line 150 def finish FileUtils.mkdir_p('doc') unless Dir.exists?('doc') render_as_html end |
#render_as_html ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/cdoc.rb', line 159 def render_as_html layout_file = File.join(File.dirname(__FILE__), 'layouts/bootstrap.html') layout = File.read(layout_file) f = File.open('doc/index.html', 'w+') html = layout % { title: @title, sidebar: @sidebar, content: content } f.write(html) f.close FileUtils.cp_r(File.join(File.dirname(__FILE__), 'styles'), 'doc/') end |
#section(str) ⇒ Object
42 43 44 45 |
# File 'lib/cdoc.rb', line 42 def section(str) template = "<p id='%{id}'><h3>%{str}</h3></p>" @content << template % { id: to_id(str), str: str } end |
#sidebar(keys) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cdoc.rb', line 30 def (keys) return if keys.empty? tmpl_item = '<a href="#%{id}" class="list-group-item">%{item}</a>' items = keys.map do |key| tmpl_item % { id: to_id(key), item: key.capitalize } end @sidebar = ['<div class="list-group">', items, '</div>'].flatten.join("\n") end |
#subsection(str) ⇒ Object
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cdoc.rb', line 47 def subsection(str) template = %q( <div class="panel panel-default" id="accounts"> <div class="panel-heading"> <h3 class="panel-title">%{section_header}</h3> </div> <div class="panel-body"> %{section_body} </div> </div>) lines = str.split("\n") index = 0 section_header = '' sub_section = [] loop do line = lines[index] if line.nil? break elsif m = line.match(/@\w+/) # tag line if it start with @<tag> block = tagged_line(line, m[0]) elsif line.start_with?(' ') # if the line start with 2 or more spaces code_block = [] code_block << line.sub(' ', '') loop do line = lines[index + 1] if line.nil? || !line.start_with?(' ') code_str = code_block.join("\n") # try to parse this as json begin json = JSON.parse(code_str) code_str = JSON.pretty_generate(json) block = highlight(code_str, 'json') rescue JSON::ParserError => e # puts e.message block = ['<pre><code>', code_str, '</code></pre>'].join("\n") end break else code_block << line.sub(' ', '') unless line.strip.empty? index = index + 1 end end elsif unordered_list?(line) list_block = [] list_block << line.sub(/\-|\*/, '').strip loop do line = lines[index + 1] if line.nil? || !unordered_list?(line) list_block.join('\n') break else list_block << line.sub(/\-|\*/, '').strip index = index + 1 end end list_block = list_block.map { |i| ['<footer>', i, '</footer>'].join('') } block = ['<blockquote>', list_block, '</blockquote>'].flatten.join("\n") else block = [line, '<br/>'].join('') end if index == 0 section_header = block else sub_section << block end index = index + 1 end @content << template % { section_header: section_header, section_body: sub_section.join("\n")} end |
#tagged_line(line, tag) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cdoc.rb', line 135 def tagged_line(line, tag) if ['url', 'endpoint', 'api'].include?(tag.downcase) # set the title of the section end t = line.sub(tag, '').strip t_l = ['<small>', tag.sub('@', '').capitalize, '</small>'] if !t.empty? t_l = t_l + ['<strong>', t.strip, '</strong>'] end ['<p>', t_l, '</p>'].flatten.join("\n") end |
#title(title) ⇒ Object
26 27 28 |
# File 'lib/cdoc.rb', line 26 def title(title) @title = title end |
#unordered_list?(line) ⇒ Boolean
130 131 132 133 |
# File 'lib/cdoc.rb', line 130 def unordered_list?(line) l = line.strip l.start_with?('- ') || l.start_with?('* ') end |