From 1c15fe67c72b4591feaceeffec0951e34a6c2e46 Mon Sep 17 00:00:00 2001 From: clsr Date: Fri, 18 Aug 2017 14:08:04 +0200 Subject: Initial commit --- .gitignore | 2 + cn-http.service | 24 ++ cnhttp.go | 645 +++++++++++++++++++++++++++++++++++++++++++++++++ static/hlraw.js | 39 +++ static/script.js | 59 +++++ static/style.css | 167 +++++++++++++ templates/content.html | 97 ++++++++ templates/page.html | 112 +++++++++ 8 files changed, 1145 insertions(+) create mode 100644 .gitignore create mode 100644 cn-http.service create mode 100644 cnhttp.go create mode 100644 static/hlraw.js create mode 100644 static/script.js create mode 100644 static/style.css create mode 100644 templates/content.html create mode 100644 templates/page.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62a5f44 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/cn-http +/static/highlight diff --git a/cn-http.service b/cn-http.service new file mode 100644 index 0000000..debeeb5 --- /dev/null +++ b/cn-http.service @@ -0,0 +1,24 @@ +[Unit] +Description=CNP-HTTP and CNM-HTML translating reverse proxy +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/cn-http -listen localhost:9000 -cnp-host contnet.org +PrivateTmp=yes +ProtectSystem=strict +ProtectHome=yes +InaccessibleDirectories=/home +ReadOnlyDirectories=/ +CapabilityBoundingSet= +LimitFSIZE=0 +DeviceAllow=/dev/null rw +MemoryDenyWriteExecute=yes +User=nobody +Group=nogroup +WorkingDirectory=/var/contnet/cn-http +Restart=always +RestartSec=10s + +[Install] +WantedBy=multi-user.target diff --git a/cnhttp.go b/cnhttp.go new file mode 100644 index 0000000..42f723d --- /dev/null +++ b/cnhttp.go @@ -0,0 +1,645 @@ +package main + +import ( + "bytes" + "errors" + "flag" + "html/template" + "io" + "log" + "net" + "net/http" + "net/url" + "path" + "sort" + "strconv" + "strings" + "time" + + "github.com/kballard/go-shellquote" + + "contnet.org/lib/cnm-go" + "contnet.org/lib/cnm-go/cnmfmt" + "contnet.org/lib/cnp-go" +) + +var ( + listen = flag.String("listen", "localhost:8080", "address for HTTP server to listen on") + cnphost = flag.String("cnp-host", "", "the CNP host to proxy (disables browser mode)") + nohighlight = flag.Bool("no-highlight", false, "do not include highlight.js scripts and stylesheets") + nostatic = flag.Bool("no-static", false, "do not serve static files on /static") + + templates = template.Must(template.New("").Funcs(map[string]interface{}{ + "inc": func(s string) string { return "" }, + "dec": func() string { return "" }, + "depth": func() int { return 0 }, + "sanchor": func() string { return "" }, + "lanchor": func() string { return "" }, + "anchor": func() string { return "" }, + "lang": func(s string) string { + return strings.Map(func(r rune) rune { + if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' || r == '-' { + return r + } + return '_' + }, s) + }, + "href": func(s string) template.HTMLAttr { + return "href=\"" + template.HTMLAttr(template.HTMLEscapeString(s)) + "\"" + }, + "cnmfmt": (func(cnmfmt.Text) template.HTML)(nil), + "tourl": (func(string) string)(nil), + }).ParseGlob("templates/*.html")) +) + +const ( + anchorCNPSchema = "cnp://" + anchorHTTPSchema = "http://" + anchorPrefix = `` +) + +var ( + anchorTemplate = template.Must(template.New("").Parse(anchorPrefix + "{{.}}" + anchorSuffix)) +) + +func escapeURL(urlStr string) string { + isCNP := len(urlStr) >= len(anchorCNPSchema) && strings.EqualFold(urlStr[:len(anchorCNPSchema)], anchorCNPSchema) + if isCNP { + urlStr = anchorHTTPSchema + urlStr[len(anchorCNPSchema):] + } + var buf bytes.Buffer + anchorTemplate.Execute(&buf, urlStr) + urlStr = buf.String() + urlStr = urlStr[len(anchorPrefix) : len(urlStr)-len(anchorSuffix)] + if isCNP { + urlStr = anchorCNPSchema + urlStr[len(anchorHTTPSchema):] + } + return urlStr +} + +type server struct { + host string + highlighting bool +} + +func newServer(host string, highlighting bool) *server { + return &server{host, highlighting} +} + +func (srv *server) handleError(w http.ResponseWriter, err error) { + code := http.StatusInternalServerError + switch err.(type) { + case cnp.ErrorDenied: + code = http.StatusForbidden + case cnp.ErrorInvalid: + code = http.StatusInternalServerError + case cnp.ErrorNotFound: + code = http.StatusNotFound + case cnp.ErrorNotSupported: + code = http.StatusInternalServerError + case cnp.ErrorRejected: + code = http.StatusUnprocessableEntity + case cnp.ErrorServerError: + code = http.StatusBadGateway + case cnp.ErrorSyntax: + code = http.StatusInternalServerError + case cnp.ErrorTooLarge: + code = http.StatusRequestEntityTooLarge + case cnp.ErrorVersion: + code = http.StatusInternalServerError + } + log.Printf("error: %T %v", err, err) + http.Error(w, http.StatusText(code), code) +} + +func (srv *server) handleIndex(w http.ResponseWriter, r *http.Request) { + req, _ := cnp.NewRequest("", "/", nil) + resp, _ := cnp.NewResponse(cnp.IntentOK, []byte{}) + srv.cnpCNMToHTML(w, r, req, resp) +} + +func (srv *server) handleCNP(w http.ResponseWriter, r *http.Request, path string) { + host := srv.host + if host == "" { + ss := strings.SplitN(strings.TrimLeft(path, "/"), "/", 2) + if len(ss) != 2 { + if len(ss[0]) > 0 { + http.Redirect(w, r, path+"/", http.StatusFound) + return + } + srv.handleIndex(w, r) + return + } + host = ss[0] + path = "/" + ss[1] + } + + req, err := cnp.NewRequest(host, path, nil) + if err != nil { + srv.handleError(w, err) + return + } + + if ims := r.Header.Get("If-Modified-Since"); ims != "" { + var t time.Time + t, err = http.ParseTime(ims) + if err == nil && !t.IsZero() { + req.SetIfModified(t) + } + } + + resp, err := cnp.Send(req) + intent := "n/a" + if resp != nil { + intent = resp.Intent() + } + log.Printf("req: %q -> %q", req.Intent(), intent) + if err != nil { + if e, ok := err.(*net.OpError); ok && e.Op == "dial" { + err = cnp.NewError(cnp.ReasonServerError) + } + srv.handleError(w, err) + return + } + + r.ParseForm() + _, preq := r.Form["req"] + _, phdr := r.Form["hdr"] + _, presp := r.Form["resp"] + _, praw := r.Form["raw"] + + if preq || phdr || presp { + srv.cnpParamsToHTTP(w, resp) + w.Header().Set("Content-Type", "text/plain") + w.Header().Del("Content-Length") + + if preq { + req.Write(w) + } + if presp { + resp.Write(w) + } else if phdr { + resp.Header.Write(w) + } + } else { + srv.cnpToWeb(w, r, req, resp, praw) + } +} + +func (srv *server) cnpToWeb(w http.ResponseWriter, r *http.Request, req *cnp.Request, resp *cnp.Response, raw bool) { + if err := resp.Validate(); err != nil { + srv.handleError(w, err) + return + } + + switch resp.ResponseIntent() { + case cnp.IntentOK: + srv.cnpParamsToHTTP(w, resp) + if !raw && resp.Type() == "text/cnm" { + srv.cnpCNMToHTML(w, r, req, resp) + } else { + io.Copy(w, resp.Body) + } + case cnp.IntentNotModified: + w.WriteHeader(http.StatusNotModified) + case cnp.IntentRedirect: + host, pth, _ := resp.Location() // already validated + loc, err := srv.cnpRedirectToHTTP(req, host, pth) + if err != nil { + srv.handleError(w, err) + return + } + log.Println("redirecting to", loc) + http.Redirect(w, r, loc, http.StatusFound) + case cnp.IntentError: + srv.handleError(w, cnp.NewError(resp.Reason())) + default: + srv.handleError(w, errors.New("unknown CNP response intent: "+resp.ResponseIntent())) + } +} + +func (srv *server) cnpRedirectToHTTP(req *cnp.Request, host, pth string) (string, error) { + var loc string + if host == "" || host == "." || host == srv.host { + loc = pth + if host == "." { + loc = path.Join(path.Dir(req.Path()), loc) + } + if srv.host == "" { + loc = path.Join("/", req.Host(), loc) + } + } else { + rr, err := cnp.NewRequest(host, pth, nil) + if err != nil { + return "", err + } + if srv.host == "" { + loc = path.Join("?", rr.Host(), rr.Path()) + } else { + loc = rr.URL().String() + } + } + if strings.HasSuffix(pth, "/") { + loc = loc + "/" + } + return loc, nil +} + +func (srv *server) cnpParamsToHTTP(w http.ResponseWriter, resp *cnp.Response) { + if l := resp.Length(); l > 0 { + w.Header().Set("Content-Length", strconv.FormatInt(l, 10)) + } + if t := resp.Type(); t != "" { + w.Header().Set("Content-Type", t) + } + if m := resp.Modified(); !m.IsZero() { + w.Header().Set("Last-Modified", m.Format(http.TimeFormat)) + } + if t := resp.Time(); !t.IsZero() { + w.Header().Set("Date", t.Format(http.TimeFormat)) + } + if n := resp.Name(); n != "" { + w.Header().Set("Content-Disposition", "inline; filename=\""+strings.Map(func(r rune) rune { + if r < ' ' || r == ' ' || r == '\'' { // filter out nulls, control codes, newlines and quotes + return -1 + } + return r + }, n)+"\"") + } +} + +type cnmPage struct { + URL string + Req string + Resp string + Doc *cnm.Document + Site []site + Netcat string + Toc tocSection + Highlight bool + Browser bool + depth int +} + +type tocSection struct { + Title string + Children []tocSection +} + +type site struct { + Path string + Name string + Children []site +} + +func genToc(b cnm.Block) []tocSection { + var res []tocSection + switch v := b.(type) { + case *cnm.SectionBlock: + if t := v.Title(); t != "" { + var ch []tocSection + for _, c := range v.Children() { + ch = append(ch, genToc(c)...) + } + res = append(res, tocSection{ + Title: v.Title(), + Children: ch, + }) + } else { + for _, bl := range v.Children() { + res = append(res, genToc(bl)...) + } + } + case *cnm.TableBlock: + for _, bl := range v.Rows() { + res = append(res, genToc(bl)...) + } + case *cnm.HeaderBlock: + for _, bl := range v.Children() { + res = append(res, genToc(bl)...) + } + case *cnm.RowBlock: + for _, bl := range v.Children() { + res = append(res, genToc(bl)...) + } + case *cnm.ContentBlock: + if v == nil { + break + } + for _, bl := range v.Children() { + res = append(res, genToc(bl)...) + } + case *cnm.ListBlock: + for _, bl := range v.Children() { + res = append(res, genToc(bl)...) + } + } + return res +} + +func cnmSite(prefix string, s cnm.Site) (st site) { + if ss := strings.Split(path.Clean(s.Path), "/"); len(ss) > 1 { + s.Path = strings.Join(ss[1:], "/") + return site{ + Path: path.Join(prefix, ss[0]), + Name: ss[0], + Children: []site{ + cnmSite(path.Join(prefix, ss[0]), s), + }, + } + } + + st.Path = path.Join(prefix, s.Path) + if s.Name == "" { + st.Name = s.Path + } else { + st.Name = s.Name + } + if len(s.Children) > 0 { + chs := make(map[string]site) + for _, ch := range s.Children { + chs[ch.Name] = cnmSite(st.Path, ch) + } + keys := make([]string, len(chs)) + i := 0 + for k := range chs { + keys[i] = k + i++ + } + sort.Slice(keys, func(i, j int) bool { + a, b := chs[keys[i]], chs[keys[j]] + da, db := strings.HasSuffix(a.Name, "/"), strings.HasSuffix(b.Name, "/") + if da && !db { + return true + } + if !da && db { + return false + } + return a.Name < b.Name + }) + st.Children = make([]site, len(keys)) + for i := range keys { + st.Children[i] = chs[keys[i]] + } + } + return +} + +func (srv *server) cnpCNMToHTML(w http.ResponseWriter, r *http.Request, req *cnp.Request, resp *cnp.Response) { + doc, err := cnm.ParseDocument(resp.Body) + if err != nil { + srv.handleError(w, err) + return + } + + w.Header().Set("Content-Type", "text/html") + w.Header().Del("Content-Length") + + var breq, bresp, buf bytes.Buffer + req.Header.Write(&breq) + resp.Header.Write(&bresp) + var st []site + if len(doc.Site.Children) > 0 { + st = []site{ + site{"/", "/", cnmSite("/", doc.Site).Children}, + } + } + + u := req.URL() + port := u.Port() + if port == "" { + port = strconv.Itoa(cnp.DefaultPort) + } + hdrs := req.Header.String() + hdrs = hdrs[:len(hdrs)-1] + host, _, _ := net.SplitHostPort(r.Host) + if host == "" && r.Host != "" { + host = r.Host + } + if host == "" { + host = r.URL.Hostname() + } + if host == "" { + host = req.Host() + } + sections := []string{} + sanchors := map[string]bool{} + lanchors := map[string]bool{} + anchor := func() template.URL { + var secs []string + for _, s := range sections { + if s = url.PathEscape(s); s != "" { + secs = append(secs, s) + } + } + return template.URL(strings.Join(secs, "/")) + } + err = templates.Funcs(map[string]interface{}{ + "inc": func(s string) string { sections = append(sections, s); return "" }, + "dec": func() string { sections = sections[:len(sections)-1]; return "" }, + "depth": func() int { + d := len(sections) + 1 + if d > 6 { + return 6 + } + return d + }, + "anchor": anchor, + "sanchor": func() template.URL { + s := url.PathEscape(sections[len(sections)-1]) + if sanchors[s] { + return "" + } + sanchors[s] = true + return template.URL(s) + }, + "lanchor": func() template.URL { + s := string(anchor()) + if lanchors[s] { + return "" + } + lanchors[s] = true + return template.URL(s) + }, + "cnmfmt": func(txt cnmfmt.Text) template.HTML { + return srv.fmtToHTML(req, txt) + }, + "tourl": func(s string) string { + u, _ := srv.linkToURL(req, s) + return u + }, + }).ExecuteTemplate(&buf, "page.html", cnmPage{ + URL: u.String(), + Req: breq.String(), + Resp: bresp.String(), + Doc: doc, + Netcat: shellquote.Join("echo", hdrs) + " | " + shellquote.Join("nc", host, port), + Site: st, + Toc: tocSection{Children: genToc(doc.Content)}, + Highlight: srv.highlighting, + Browser: srv.host == "", + }) + if err != nil { + srv.handleError(w, err) + return + } + + io.Copy(w, &buf) +} + +func (srv *server) fmtToHTML(req *cnp.Request, text cnmfmt.Text) template.HTML { + if len(text.Spans) == 0 { + return "" + } + + var last cnmfmt.Span + var buf bytes.Buffer + hadText := true + + spans := make([]cnmfmt.Span, len(text.Spans)+1) + copy(spans, text.Spans) + spans[len(spans)-1] = cnmfmt.Span{} // end all formats + + var tags []string + for _, span := range spans { + if last.Format.Link != span.Format.Link { + if !hadText && last.Format.Link != "" { // no text in link + template.HTMLEscape(&buf, []byte(last.Format.Link)) + } + hadText = false + } + srv.handleTags(req, &buf, &tags, last.Format, span.Format) + if span.Text != "" { + hadText = true + template.HTMLEscape(&buf, []byte(span.Text)) + } + last = span + } + + return template.HTML("

" + buf.String() + "

") +} + +func (srv *server) handleTags(req *cnp.Request, buf *bytes.Buffer, tags *[]string, old, new cnmfmt.Format) { + if old == new { + return + } + + close := map[string]bool{ + "b": old.Bold && !new.Bold, + "i": old.Italic && !new.Italic, + "u": old.Underline && !new.Underline, + "code": old.Monospace && !new.Monospace, + "a": old.Link != "" && old.Link != new.Link, + } + + open := map[string]bool{ + "b": !old.Bold && new.Bold, + "i": !old.Italic && new.Italic, + "u": !old.Underline && new.Underline, + "code": !old.Monospace && new.Monospace, + "a": new.Link != "" && old.Link != new.Link, + } + + t := *tags + + pop := len(t) + for i := len(t) - 1; i >= 0; i-- { + if close[t[i]] { + pop = i + } + } + for i := len(t) - 1; i >= pop; i-- { + if !close[t[i]] { + open[t[i]] = true + } + buf.WriteString("") + } + *tags = t[:pop] + + tagPush(buf, tags, open, "b", "") + tagPush(buf, tags, open, "i", "") + tagPush(buf, tags, open, "u", "") + tagPush(buf, tags, open, "code", "") + + link, extern := srv.linkToURL(req, new.Link) + tagPush(buf, tags, open, "a", "") +} + +func (srv *server) linkToURL(req *cnp.Request, link string) (urlStr, extern string) { + urlStr = "#ZcnpfmtZ" + host := srv.host + if host == "" { + host = req.Host() + } + if u, err := url.Parse(link); err == nil { + if u.Scheme == "cnp" { + var lhost, lpath string + lhost = u.Host + lpath = u.Path + if u.Host == srv.host { + lhost = "" + } else { + extern = " class=\"cnp-external cnp-external-cnp\"" + } + urlStr = path.Join("/", lhost, lpath) + if strings.HasSuffix(link, "/") { + urlStr += "/" + } + if u.Fragment != "" { + urlStr += "#" + u.Fragment + } + } else if u.Scheme != "" { + extern = " class=\"cnp-external cnp-external-" + template.HTMLEscapeString(u.Scheme) + "\"" + urlStr = u.String() + } else { + urlStr = u.Path + if srv.host == "" { + if !strings.HasPrefix(urlStr, "/") { + urlStr = req.Path() + "/" + urlStr + } + urlStr = "/" + host + cnp.Clean("/"+urlStr) + } + if u.Fragment != "" { + urlStr += "#" + u.Fragment + } + } + } + return +} + +func tagPush(buf *bytes.Buffer, tags *[]string, ok map[string]bool, name, tag string) { + if ok[name] { + buf.WriteString(tag) + *tags = append(*tags, name) + } +} + +func (srv *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Printf("access: %s %s %q", r.RemoteAddr, r.Method, r.URL) + if r.Method == http.MethodPost { + u := r.FormValue("url") + req, err := cnp.NewRequestURL(u, nil) + if err != nil { + http.Error(w, err.Error(), http.StatusUnprocessableEntity) + return + } + http.Redirect(w, r, path.Join("/", req.Host(), req.Path()), http.StatusFound) + } else { + srv.handleCNP(w, r, r.URL.Path) + } +} + +func main() { + flag.Parse() + + srv := newServer(*cnphost, !*nohighlight) + + if !*nostatic { + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + } + http.Handle("/", srv) + + log.Printf("listening on %s", *listen) + panic(http.ListenAndServe(*listen, nil)) +} diff --git a/static/hlraw.js b/static/hlraw.js new file mode 100644 index 0000000..8d57bb4 --- /dev/null +++ b/static/hlraw.js @@ -0,0 +1,39 @@ +(function() { + 'use strict'; + + var highlight = function(block, lang) { + var langs = lang.split('_'); + if (langs.length > 1) { + lang = langs[langs.length-1]; + } + if (!lang || typeof hljs.getLanguage(lang) === 'undefined') { + return; + } + var hl = hljs.highlight(lang, block.textContent, true); + block.innerHTML = hl.value; + }; + + var highlightAll = function() { + var rawBlocks = document.querySelectorAll('pre.cnm-raw code'); + for (var i=0; idetails'); + for (var i=0; i 0) { + toggle.open = true; + clickToggle(); + for (var i=0; i 0) { + var main = document.querySelector('main'); + main.insertBefore(toggle, main.firstChild); + } + + }; + + if (document.readyState !== 'loading') { + init(); + } else { + document.addEventListener('DOMContentLoaded', init); + } +})(); diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..752d538 --- /dev/null +++ b/static/style.css @@ -0,0 +1,167 @@ +html { + height: 100%; + font-family: sans-serif; + color: black; + background-color: white; +} + +body { + margin: 1em auto; + max-width: 50em; + padding: 0 0.5em; +} + +section { + margin: 0.25em; +} + +section h1, section h2, section h3, +section h4, section h5, section h6 { + padding: 0; + margin: 0; + display: inline-block; +} + +.sec-link { + display: none; +} + +:hover>*>.sec-link { + display: inline; +} + +a.cnp-external:after { + text-decoration: none; + color: gray; + display: inline-block; + font-size: 0.8em; + content: '\01f517'; +} + +a.cnp-external-cnp:after { + content: '[cnp]'; +} + +a.cnp-external-http:after { + content: '[http]'; +} + +a.cnp-external-https:after { + content: '[https]'; +} + +main { + border: 1px dashed #aaa; + tab-size: 4; + -moz-tab-size: 4; /* Pale Moon and older FF need prefix */ +} + +main, section { + padding: 0.75em; +} + +main:hover, section:hover { +} + +pre, code { + font-family: monospace, monospace; + font-size: 1em; +} + +pre { + margin: 0; + display: inline-block; + white-space: pre-wrap; +} + +code { + background-color: #f8f8f8; + border: 1px solid #ccc; +} + +pre.cnm-raw { + display: block; +} + +pre>code { + background-color: #fbfbfb; + display: block; + border: 1px solid black; + width: auto; + padding: 0.5em; + margin-top: 0.5em; + margin-bottom: 0.5em; +} + +pre>code:hover { + background-color: #f4f4f4; +} + +p, figcaption, pre { + margin-bottom: 1em; +} + +figure { + border: 1px solid #aaa; + display: inline-block; + padding: 0 0.5em; + margin: 0 auto; +} + +summary { + cursor: pointer; +} + +details>summary>* { + text-decoration: underline; +} + +details[open]>summary>* { + text-decoration: none; +} + +header { +} + +footer { + padding-top: 0.5em; + font-size: 0.8em; +} + +footer details { + display: inline-block; +} + +table { + margin-top: 0.5em; + margin-bottom: 0.5em; + border-collapse: collapse; +} + +li p, li pre, li code, table p, table pre, table code { + padding: 0; + margin: 0; +} + +table, th, td { + border: 1px solid black; +} + +img { + padding-top: 0.5em; + max-width: 100%; +} + +#browser { + width: 100%; + border: 1px solid black; + padding: 1em; + display: flex; +} +#browser input[type=text] { + flex: 1; +} +#browser input[type=submit] { + border: 1px solid black; + margin-left: 0.5em; +} diff --git a/templates/content.html b/templates/content.html new file mode 100644 index 0000000..73f18c7 --- /dev/null +++ b/templates/content.html @@ -0,0 +1,97 @@ +{{- if eq .Name "section" "content" -}} +{{- if .Title}} + +{{else -}} +{{- range .Children -}} + {{- template "content.html" . -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{- if eq .Name "list" -}} +{{if .Ordered}} + {{- range .Children -}} + {{- if eq .Name "list" -}} + {{- template "content.html" . -}} + {{- else -}} +
  • {{- template "content.html" . -}}
  • + {{- end -}} + {{- end -}} +{{- if .Ordered}}{{else}}{{end -}} +{{- end -}} + +{{- if eq .Name "table" -}} + + {{- range .Rows -}} + {{- template "content.html" . -}} + {{- end -}} +
    +{{- end -}} + +{{- if eq .Name "header" -}} + + {{- range .Children -}} + {{template "content.html" .}} + {{- end -}} + +{{- end -}} + +{{- if eq .Name "row" -}} + + {{- range .Children -}} + {{template "content.html" .}} + {{- end -}} + +{{- end -}} + +{{- if eq .Name "embed" -}} +
    + {{- if eq .Type "image/png" "image/jpeg" "image/webp" "image/*" -}} + {{with .Description}}{{.}}{{else}}embedded {{.Type}}{{end}} + {{- end -}} + {{- if eq .Type "video/mp4" "video/webm" "video/*" -}} + + {{- end -}} + {{- if not (eq .Type "image/png" "image/jpeg" "image/webp" "image/*" "video/mp4" "video/webm" "video/*") -}} +

    + Embedded {{.Type}} content: {{.URL}} +

    + {{- end -}} + {{- with .Description}}
    {{.}}
    {{end -}} +
    +{{- end -}} + +{{- if eq .Name "text" -}} + {{- if eq .Format "" "plain"}}{{range .Contents.Paragraphs -}} +

    {{.}}

    + {{- end}}{{end -}} + {{- if eq .Format "fmt" -}} + {{- range .Contents.Paragraphs -}} + {{- cnmfmt . -}} + {{- end -}} + {{- end -}} + {{- if not (eq .Format "" "plain" "fmt") -}} +
    {{.Contents.Text}}
    + {{- end -}} +{{- end -}} + +{{- if eq .Name "raw" -}} +
    {{.Contents}}
    +{{- end -}} diff --git a/templates/page.html b/templates/page.html new file mode 100644 index 0000000..0f89e2d --- /dev/null +++ b/templates/page.html @@ -0,0 +1,112 @@ + + + + + + {{with .Doc.Title}}{{.}}{{else}} {{end}} + + + + {{if .Highlight}}{{end}} + + +
    + {{if .Browser -}} +
    + + +
    + {{- end}} + + {{with .Doc.Title}}

    {{.}}

    {{end -}} + + {{with .Doc.Links -}} + + {{- end -}} + + {{with .Site -}} + + {{- end -}} + + {{if .Toc.Children -}} + + {{- end}} +
    + +
    + {{- with .Doc.Content}} + {{- range .Children}} + {{- template "content.html" .}} + {{- end}}{{end}} +
    + + + + {{if .Highlight}} + {{end}} + + -- cgit
    + {{- inc .Title -}} + + {{- with .Title -}} + {{- if $l := lanchor -}} + {{.}}ΒΆ + {{- if $s := sanchor}}{{if ne $s $l}}{{end}}{{end -}} + + {{- end -}} + {{- end -}} + + {{- range .Children -}} + {{- template "content.html" . -}} + {{- end -}} + {{- dec -}} +