Class: Cdoc::DocString

Inherits:
Object
  • Object
show all
Defined in:
lib/cdoc.rb

Instance Method Summary collapse

Constructor Details

#initializeDocString

Returns a new instance of DocString.



17
18
19
# File 'lib/cdoc.rb', line 17

def initialize
  @docstring = ''
end

Instance Method Details

#finishObject



88
89
90
91
92
# File 'lib/cdoc.rb', line 88

def finish
  FileUtils.mkdir_p('doc') unless Dir.exists?('doc')
  render_as_markdown
  render_as_html
end

#render_as_htmlObject



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

def render_as_html
  layout_file = File.join(File.dirname(__FILE__), 'layouts/default.html')
  layout = File.read(layout_file)
  f = File.open('doc/index.html', 'w+')
  renderer = DocRenderer.new
  markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true)
  html = layout % { title: @title, content: markdown.render(@docstring)}
  f.write(html)
  f.close

  unless Dir.exists?('doc/css')
    puts File.join(File.dirname(__FILE__), 'styles')
    FileUtils.cp_r(File.join(File.dirname(__FILE__), 'styles'), 'doc/css')
  end
end

#render_as_markdownObject



94
95
96
97
98
# File 'lib/cdoc.rb', line 94

def render_as_markdown
  f = File.open('doc/index.md', 'w+')
  f.write(@docstring)
  f.close
end

#section(str) ⇒ Object



26
27
28
# File 'lib/cdoc.rb', line 26

def section(str)
  @docstring << "\n" + "## #{str}" + "\n"
end

#subsection(str) ⇒ Object



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
# File 'lib/cdoc.rb', line 30

def subsection(str)
  lines = str.split("\n")
  index = 0

  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 = ["\n", '```json', code_str, '```'].join("\n")
          rescue JSON::ParserError => e
            puts e.message
            block = ["\n", '```', code_str, '```'].join("\n")
          end

          break
        else
          code_block << line.sub('  ', '')
          index = index + 1
        end
      end
    else
      block = line
    end

    @docstring << block
    index = index + 1
  end

  @docstring << "\n"
end

#tagged_line(line, tag) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/cdoc.rb', line 77

def tagged_line(line, tag)
  t   = line.sub(tag, '').strip
  t_l = "\n" + tag.sub('@','').capitalize

  if !t.empty?
    t_l = t_l + " **#{t.strip}**\n\n"
  end

  t_l
end

#title(title) ⇒ Object



21
22
23
24
# File 'lib/cdoc.rb', line 21

def title(title)
  @title = title
  @docstring << "\n" + "# #{title}" + "\n"
end