1 #!/usr/bin/env ruby
     
2 
     3 fpath = ARGV[0]
     
4 fname_rel = ARGV[1]
     
5 
     6 # make relative link for root of "site" and shared CSS
     
7 root_rel_link = '../' * fname_rel.count('/')
     
8 
     9 puts <<HTML
    
10 <!DOCTYPE html>
    
11 <html lang="en">
    
12 <head>
    
13     <meta charset="utf-8">
    
14     <meta name="viewport" content="width=device-width, initial-scale=1">
    
15     <title>#{fname_rel} - Zig standard library</title>
    
16     <link rel="stylesheet" href="#{root_rel_link}styles.css">
    
17 </head>
    
18 <body>
    
19 <table><tbody>
    
20 <tr><td class="doc">
    
21 <h1>
    
22   <a href="index.html"><img src="#{root_rel_link}zig-stdlib-book.svg" alt="" width="60"> zig/lib/std</a> /
    
23   #{fname_rel}
    
24 </h1>
    
25 HTML
    
26 
    27 # used to collect documentation comments, line-by-line
    
28 doc_comment = nil
    
29 
    30 # we always start the page in the documentation column
    
31 in_code_block = false
    
32 
    33 def new_chunk(extra_class='')
    
34   puts '</pre></td></tr>' # end code block
    
35   puts "<tr><td class=\"doc #{extra_class}\">"
    
36 end
    
37 
    38 File.read(fpath).each_line do |line|
    
39   # Doc comment (/// or //!), we must gather it!
    
40   if comment = line.match(/^\s*\/{2}[!\/](.*)$/)
    
41     comment = comment[1]
    
42     if comment.match?(/^\s*$/)
    
43       comment = "<br><br>"
    
44     end
    
45 
    46     # replace all `foo` with <code>foo</code>
    
47     comment.gsub!(/`([^`]+)`/, '<code>\1</code>')
    
48 
    49     if doc_comment
    
50       doc_comment += comment
    
51     else
    
52       doc_comment = comment
    
53     end
    
54 
    55     next
    
56   end
    
57 
    58   # When we encounter a special document thing, we'll store it here.
    
59   my_special_thing = nil
    
60 
    61   # This is will just be used as a CSS class name for formatting. Blank
    
62   # means default formatting.
    
63   my_chunk_type = ""
    
64 
    65   # Detect Special Documentation Stuff and Print 'Em!
    
66   # --------------------------------------------------------------------------
    
67 
    68   # pub const
    
69   if name = line.match(/^pub const (\w+)/)
    
70     my_chunk_type = 'value'
    
71 
    72     my_special_thing = "<h2>#{name[1]}</h2>"
    
73 
    74     if fname = line.match(/@import\("(.*zig)"\)/)
    
75       my_special_thing += "<a href=\"#{fname[1]}.html\">#{fname[1]}</a>"
    
76     end
    
77   end
    
78 
    79   # pub fn
    
80   if name = line.match(/^pub( inline)? fn (\w+)/)
    
81     my_special_thing = "<h2>#{name[2]}()</h2>"
    
82   end
    
83 
    84   # struct/enum method pub fn
    
85   if name = line.match(/^\s+pub( inline)? fn (\w+)/)
    
86     my_chunk_type = 'method'
    
87     my_special_thing = "<h2>#{name[2]}()</h2>"
    
88   end
    
89 
    90   # test with "string" description
    
91   if name = line.match(/^\s*test "(.*)" {$/)
    
92     my_chunk_type = 'test-str'
    
93     my_special_thing = "<h2>Test:</h2><h3>#{name[1]}</h3>"
    
94   end
    
95 
    96   # test with Decl description
    
97   if name = line.match(/^\s*test (\w+) {$/)
    
98     my_chunk_type = 'test-decl'
    
99     my_special_thing = "<h2>Test: #{name[1]}</h2>"
   
100   end
   
101 
   102   # Print any non-comment thing detected above
   
103   if my_special_thing
   
104     if in_code_block
   
105       puts '</pre></td></tr>' # end code block
   
106       puts "<tr><td class=\"doc #{my_chunk_type}\">"
   
107     end
   
108 
   109     puts my_special_thing
   
110 
   111     if doc_comment
   
112       puts "<p>#{doc_comment}</p>"
   
113       doc_comment = nil
   
114     end
   
115 
   116     in_code_block = false
   
117   end
   
118 
   119   # Everything Else is Code!
   
120   # --------------------------------------------------------------------------
   
121   if !in_code_block
   
122     in_code_block = true
   
123 
   124     if doc_comment
   
125       puts "<p>#{doc_comment}</p>"
   
126       doc_comment = nil
   
127     end
   
128 
   129     puts '</td>' #end the doc cell
   
130     puts '<td class="code"><pre>'
   
131   end
   
132 
   133   # the actual line of source!
   
134   puts line
   
135 end
   
136 
   137 # end final code block and footer it up!
   
138 puts <<HTML
   
139 </pre></td></tr>
   
140 <tr><td class="doc" id="footer">
   
141   Generated by <a href="http://ratfactor.com/repos/zstd-browse2/">zstd-browse2</a>
   
142   on #{Time.now}.
   
143 </td><td class="code"></td></tr>
   
144 </tbody></table>
   
145 </body>
   
146 </html>
   
147 HTML