Mercurial Hosting > qwb
comparison qwb.go @ 4:ce2b6dde4c10
remove nesting restriction + per-page header
| author | Atarwn Gard <a@qwa.su> |
|---|---|
| date | Sat, 14 Mar 2026 14:01:51 +0500 |
| parents | 3222f88c0afe |
| children | 125e599b1217 |
comparison
equal
deleted
inserted
replaced
| 3:f1209af34e3d | 4:ce2b6dde4c10 |
|---|---|
| 194 } | 194 } |
| 195 total := len(s.pages) | 195 total := len(s.pages) |
| 196 if s.index != "" { | 196 if s.index != "" { |
| 197 total++ | 197 total++ |
| 198 } | 198 } |
| 199 if total < 2 { | |
| 200 break | |
| 201 } | |
| 202 b.WriteString("<ul>\n") | 199 b.WriteString("<ul>\n") |
| 203 if s.index != "" { | 200 if s.index != "" { |
| 204 navlink(&b, page{"Index", s.index}, cur) | 201 navlink(&b, page{"Index", s.index}, cur) |
| 205 } | 202 } |
| 206 for _, p := range s.pages { | 203 for _, p := range s.pages { |
| 220 if p.path == cur { | 217 if p.path == cur { |
| 221 return true | 218 return true |
| 222 } | 219 } |
| 223 } | 220 } |
| 224 return false | 221 return false |
| 222 } | |
| 223 | |
| 224 func extracth1(html string) (title, rest string) { | |
| 225 start := strings.Index(html, "<h1") | |
| 226 if start == -1 { | |
| 227 return "", html | |
| 228 } | |
| 229 close := strings.Index(html[start:], ">") | |
| 230 if close == -1 { | |
| 231 return "", html | |
| 232 } | |
| 233 content := start + close + 1 | |
| 234 end := strings.Index(html[content:], "</h1>") | |
| 235 if end == -1 { | |
| 236 return "", html | |
| 237 } | |
| 238 title = html[content : content+end] | |
| 239 rest = html[:start] + html[content+end+len("</h1>"):] | |
| 240 return | |
| 225 } | 241 } |
| 226 | 242 |
| 227 func mdtohtml(path string) (string, error) { | 243 func mdtohtml(path string) (string, error) { |
| 228 cmd := exec.Command("lowdown", "-T", "html", "--html-no-skiphtml", "--html-no-escapehtml") | 244 cmd := exec.Command("lowdown", "-T", "html", "--html-no-skiphtml", "--html-no-escapehtml") |
| 229 f, err := os.Open(path) | 245 f, err := os.Open(path) |
| 316 body, err := mdtohtml(path) | 332 body, err := mdtohtml(path) |
| 317 if err != nil { | 333 if err != nil { |
| 318 return err | 334 return err |
| 319 } | 335 } |
| 320 body = fixlinks(body) | 336 body = fixlinks(body) |
| 337 pageTitle, body := extracth1(body) | |
| 338 if pageTitle == "" { | |
| 339 pageTitle = cfg.headertext | |
| 340 } | |
| 321 cur := "/" + strings.TrimSuffix(rel, ".md") + ".html" | 341 cur := "/" + strings.TrimSuffix(rel, ".md") + ".html" |
| 322 title := cfg.headertext | 342 title := cfg.headertext |
| 323 if filepath.Base(path) != "index.md" { | 343 if filepath.Base(path) != "index.md" { |
| 324 title = cfg.headertext + " | " + titlefromname(filepath.Base(path)) | 344 title = cfg.headertext + " | " + pageTitle |
| 325 } | 345 } |
| 326 pg:= strings.ReplaceAll(tmpl, "{{TITLE}}", title) | 346 pg := strings.ReplaceAll(tmpl, "{{TITLE}}", title) |
| 327 pg = strings.ReplaceAll(pg, "{{SITE_TITLE}}", cfg.headertext) | 347 pg = strings.ReplaceAll(pg, "{{SITE_TITLE}}", pageTitle) |
| 328 pg = strings.ReplaceAll(pg, "{{FOOTER_TEXT}}", cfg.footertext) | 348 pg = strings.ReplaceAll(pg, "{{FOOTER_TEXT}}", cfg.footertext) |
| 329 pg = strings.ReplaceAll(pg, "{{CSS}}", cfg.cssfile) | 349 pg = strings.ReplaceAll(pg, "{{CSS}}", cfg.cssfile) |
| 330 pg = strings.ReplaceAll(pg, "{{NAV}}", buildnav(rootsec, subs, cur)) | 350 pg = strings.ReplaceAll(pg, "{{NAV}}", buildnav(rootsec, subs, cur)) |
| 331 pg = strings.ReplaceAll(pg, "{{CONTENT}}", body) | 351 pg = strings.ReplaceAll(pg, "{{CONTENT}}", body) |
| 332 outpath = strings.TrimSuffix(outpath, ".md") + ".html" | 352 outpath = strings.TrimSuffix(outpath, ".md") + ".html" |
