2021-01-08 13:25:22 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import strings
|
|
|
|
import v.doc
|
|
|
|
|
2021-01-08 19:44:19 +03:00
|
|
|
fn markdown_escape_script_tags(str string) string {
|
|
|
|
return str.replace_each(['<script>', '`', '</script>', '`'])
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:25:22 +03:00
|
|
|
fn (vd VDoc) gen_markdown(d doc.Doc, with_toc bool) string {
|
|
|
|
mut hw := strings.new_builder(200)
|
|
|
|
mut cw := strings.new_builder(200)
|
2022-11-15 16:53:13 +03:00
|
|
|
hw.writeln('# ${d.head.content}\n')
|
2021-01-08 13:25:22 +03:00
|
|
|
if d.head.comments.len > 0 {
|
|
|
|
comments := if vd.cfg.include_examples {
|
|
|
|
d.head.merge_comments()
|
|
|
|
} else {
|
|
|
|
d.head.merge_comments_without_examples()
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
hw.writeln('${comments}\n')
|
2021-01-08 13:25:22 +03:00
|
|
|
}
|
|
|
|
if with_toc {
|
|
|
|
hw.writeln('## Contents')
|
|
|
|
}
|
|
|
|
vd.write_markdown_content(d.contents.arr(), mut cw, mut hw, 0, with_toc)
|
|
|
|
footer_text := gen_footer_text(d, !vd.cfg.no_timestamp)
|
2022-11-15 16:53:13 +03:00
|
|
|
cw.writeln('#### ${footer_text}')
|
2021-01-08 13:25:22 +03:00
|
|
|
return hw.str() + '\n' + cw.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (vd VDoc) write_markdown_content(contents []doc.DocNode, mut cw strings.Builder, mut hw strings.Builder, indent int, with_toc bool) {
|
|
|
|
for cn in contents {
|
|
|
|
if with_toc && cn.name.len > 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
hw.writeln(' '.repeat(2 * indent) + '- [${slug(cn.name)}](#${cn.name})')
|
|
|
|
cw.writeln('## ${cn.name}')
|
2021-01-08 13:25:22 +03:00
|
|
|
}
|
|
|
|
if cn.content.len > 0 {
|
|
|
|
comments := cn.merge_comments_without_examples()
|
2022-11-15 16:53:13 +03:00
|
|
|
cw.writeln('```v\n${cn.content}\n```\n${comments}\n')
|
2021-01-08 13:25:22 +03:00
|
|
|
// Write examples if any found
|
|
|
|
examples := cn.examples()
|
|
|
|
if vd.cfg.include_examples && examples.len > 0 {
|
|
|
|
example_title := if examples.len > 1 { 'Examples' } else { 'Example' }
|
2022-11-15 16:53:13 +03:00
|
|
|
cw.writeln('${example_title}\n```v\n')
|
2021-01-08 13:25:22 +03:00
|
|
|
for example in examples {
|
2022-11-15 16:53:13 +03:00
|
|
|
cw.writeln('${example}\n')
|
2021-01-08 13:25:22 +03:00
|
|
|
}
|
|
|
|
cw.writeln('```\n')
|
|
|
|
}
|
2021-07-27 12:37:40 +03:00
|
|
|
cw.writeln(r'[[Return to contents]](#Contents)')
|
|
|
|
cw.writeln('')
|
2021-01-08 13:25:22 +03:00
|
|
|
}
|
|
|
|
vd.write_markdown_content(cn.children, mut cw, mut hw, indent + 1, with_toc)
|
|
|
|
}
|
|
|
|
}
|