1use crate::entry::{Entry, Kind};
13
14pub fn render_markdown(entry: &Entry) -> String {
15 let mut sections: Vec<String> = Vec::new();
20
21 sections.push(format!("# {}\n\n_{}_", entry.label, kind_label(entry.kind)));
23
24 if let Some(sig) = entry.signature {
25 sections.push(format!("```sql\n{}\n```", sig.trim_end()));
26 }
27
28 if !entry.doc.is_empty() {
29 sections.push(wrap_paragraphs(entry.doc.trim_end(), 72));
33 }
34
35 if !entry.example.is_empty() {
36 sections.push(format!("**Example**\n\n```sql\n{}\n```", entry.example.trim_end()));
37 }
38
39 if !entry.url.is_empty() {
40 sections.push(format!("[Postgres docs]({})", entry.url));
41 }
42
43 let mut out = sections.join("\n\n");
45 out.push('\n');
46 out
47}
48
49fn kind_label(kind: Kind) -> &'static str {
50 match kind {
51 Kind::Keyword => "Keyword",
52 Kind::Function => "Function",
53 Kind::Type => "Type",
54 }
55}
56
57pub fn wrap_paragraphs(text: &str, width: usize) -> String {
61 let mut out = String::with_capacity(text.len() + 16);
62 let mut in_fence = false;
63 let mut first_line = true;
64 for line in text.split('\n') {
65 if !first_line {
66 out.push('\n');
67 }
68 first_line = false;
69 let trimmed = line.trim_start();
70 if trimmed.starts_with("```") {
71 in_fence = !in_fence;
72 out.push_str(line);
73 continue;
74 }
75 if in_fence
78 || line.starts_with(" ")
79 || line.starts_with('\t')
80 || line.starts_with('|')
81 || line.starts_with('-')
82 || line.starts_with('*')
83 || line.starts_with('>')
84 || line.trim().is_empty()
85 {
86 out.push_str(line);
87 continue;
88 }
89 out.push_str(&wrap_one(line, width));
90 }
91 out
92}
93
94fn wrap_one(line: &str, width: usize) -> String {
95 let mut out = String::with_capacity(line.len() + 8);
96 let mut col = 0usize;
97 let mut first = true;
98 for word in line.split_whitespace() {
99 let w = word.chars().count();
100 if first {
101 out.push_str(word);
102 col = w;
103 first = false;
104 continue;
105 }
106 if col + 1 + w > width {
107 out.push('\n');
108 out.push_str(word);
109 col = w;
110 } else {
111 out.push(' ');
112 out.push_str(word);
113 col += 1 + w;
114 }
115 }
116 out
117}