]> Cypherpunks repositories - gostls13.git/commitdiff
godoc: specify HTML page metadata with a JSON blob
authorAndrew Gerrand <adg@golang.org>
Thu, 19 Jan 2012 00:24:54 +0000 (11:24 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 19 Jan 2012 00:24:54 +0000 (11:24 +1100)
This allows HTML pages to specify arbitrary data in a header:

<!--{
        "Title": "The page title",
        ...
}-->

replacing the old style comments:

<!-- title The page title -->

R=gri, rsc, r, bradfitz, dsymonds
CC=golang-dev
https://golang.org/cl/5532093

28 files changed:
doc/articles/defer_panic_recover.html
doc/articles/defer_panic_recover.tmpl
doc/articles/error_handling.html
doc/articles/error_handling.tmpl
doc/articles/slices_usage_and_internals.html
doc/articles/slices_usage_and_internals.tmpl
doc/code.html
doc/codereview_with_mq.html
doc/community.html
doc/contrib.html
doc/contribute.html
doc/debugging_with_gdb.html
doc/docs.html
doc/effective_go.html
doc/effective_go.tmpl
doc/gccgo_contribute.html
doc/gccgo_install.html
doc/go1.html
doc/go1.tmpl
doc/go_faq.html
doc/go_for_cpp_programmers.html
doc/go_mem.html
doc/go_spec.html
doc/go_tutorial.html
doc/go_tutorial.tmpl
doc/install.html
doc/playground.html
src/cmd/godoc/godoc.go

index b78258d1c8dc74bc8f4de429cf4b1cb990b09f6e..18c0de2d6c636b0e8af5ac0741f2ca6740396b87 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Defer, Panic, and Recover -->
+<!--{
+       "Title": "Defer, Panic, and Recover"
+}-->
 <!--
   DO NOT EDIT: created by
     tmpltohtml articles/defer_panic_recover.tmpl
index 780040a7d86845b41065dbb1f59ac8cd339bc2f2..60c8eebe0b39db2ac6661eb44f9de54a4f18d94a 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Defer, Panic, and Recover -->
+<!--{
+       "Title": "Defer, Panic, and Recover"
+}-->
 {{donotedit}}
 <p>
 Go has the usual mechanisms for control flow: if, for, switch, goto.  It also
index 89f29983d1b6d7a3c24792204ac8987b5fbcae84..b9393a2cbaf8d5500613351f2092ba29d6aa4fc3 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Error Handling and Go -->
+<!--{
+       "Title": "Error Handling and Go"
+}-->
 <!--
   DO NOT EDIT: created by
     tmpltohtml articles/error_handling.tmpl
index 508885a8fdc788df3dc51971fd673dad81ec150d..141b4a54dd18a35da3e7cf417aabc7b891b70118 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Error Handling and Go -->
+<!--{
+       "Title": "Error Handling and Go"
+}-->
 {{donotedit}}
 <p>
 If you have written any Go code you have probably encountered the built-in
index 66ca4a2b548be2da31344bf6e7a13f2787dd4fe6..c10dfe0cadd1e9d386912d369ce7df3619e0c445 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Slices: usage and internals -->
+<!--{
+       "Title": "Slices: usage and internals"
+}-->
 <!--
   DO NOT EDIT: created by
     tmpltohtml articles/slices_usage_and_internals.tmpl
index 94929818b01b9dd79912f4c01c33d177850a8e53..d2f8fb7f58a26eb7d488e80242f3701c1b07b6ef 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Slices: usage and internals -->
+<!--{
+       "Title": "Slices: usage and internals"
+}-->
 {{donotedit}}
 
 <p>
index 4737a38fd81a9f22d1ad44210b61278d9a38178a..76d25e13ae8bd7b89fe64e7a2878b9b427909c7c 100644 (file)
@@ -1,4 +1,6 @@
-<!-- How to Write Go Code -->
+<!--{
+       "Title": "How to Write Go Code"
+}-->
 
 <h2 id="Introduction">Introduction</h2>
 
index 33f415f1398e2b53e151bfb22ce637cd9cecefa4..38ecbda6e72512cc9551fadbfd06de98debffcde 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Using Mercurial Queues with Codereview -->
+<!--{
+       "Title": "Using Mercurial Queues with Codereview"
+}-->
 
 <h2 id="Introduction">Introduction</h2>
 
index c3b16cacbc34df2ec082be79444151118f2f1417..aee731e3c05c344e8726a8a2239c3f57372d9577 100644 (file)
@@ -1,4 +1,6 @@
-<!-- title Community -->
+<!--{
+       "Title": "Community"
+}-->
 
 <div class="left-column">
 
index 5e00345c8eaee1d622c3a4c4b1bf0f5f4171a4ec..3ef7d5695aef5cee25337ee4151595afa0921299 100644 (file)
@@ -1,4 +1,6 @@
-<!-- title Contributing -->
+<!--{
+       "Title": "Contributing"
+}-->
 
 <div class="left-column">
 
index a5beb54f9b5a4441dd3f19a0a829752cc46845ac..5af9af48e5e0eb7254d4e0f43e4ca532cfe24ece 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Contribution Guidelines -->
+<!--{
+       "Title": "Contribution Guidelines"
+}-->
 
 <h2 id="Introduction">Introduction</h2>
 
index 874c468345f4d0a53393c77c6ebfab66e81c5d47..d5c1a884c08da981f6790f91ff78dea010179411 100644 (file)
@@ -1,4 +1,6 @@
-<!-- title Debugging Go Code with GDB -->
+<!--{
+       "Title": "Debugging Go Code with GDB"
+}-->
 
 <p><i>
 This applies to the 6g toolchain. Gccgo has native gdb support. Besides this
index 5a5c8ee131f845820ae2fb3f572458d743460098..c0ced98de4e16560ff49cb22766c948b9709664a 100644 (file)
@@ -1,4 +1,6 @@
-<!-- title Documentation -->
+<!--{
+       "Title": "Documentation"
+}-->
 
 <div class="left-column">
 
index 3c16e10c3a71bcea76ff20997fc611ebb5ef733e..4ecbe19b9ff5c3f6459efd3207faa68d3e7a4adb 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Effective Go -->
+<!--{
+       "Title": "Effective Go"
+}-->
 <!--
   DO NOT EDIT: created by
     tmpltohtml effective_go.tmpl
index af1bc1ea4391dd4b8e0aaa4e47c08cdbf51752fb..782c7003e730f9ce293251f80407e03a795f4c07 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Effective Go -->
+<!--{
+       "Title": "Effective Go"
+}-->
 {{donotedit}}
 
 <h2 id="introduction">Introduction</h2>
index 8eeb3a5c5cffb6044e2c7d59ca79a424445b95c5..b641c1abb552ac1b30ad7cf6644b5963b3e33cfb 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Contributing to the gccgo frontend -->
+<!--{
+       "Title": "Contributing to the gccgo frontend"
+}-->
 
 <h2>Introduction</h2>
 
index 38a30e9c62bf37d2b304fa300101ec7fdc3d7f3d..1aaf64e54e0c90d73ad15518af5d842ca99c4e6a 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Setting up and using gccgo -->
+<!--{
+       "Title": "Setting up and using gccgo"
+}-->
 
 <p>
 This document explains how to use <code>gccgo</code>, a compiler for
index 4b985071ef638257dbe76867030cf019fb3ef88e..f116ee209a674947b911d276f756c7960d87b5d7 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Go 1 Release Notes -->
+<!--{
+       "Title": "Go 1 Release Notes"
+}-->
 <!--
   DO NOT EDIT: created by
     tmpltohtml go1.tmpl
index 8d295d6e4548d87cfe2aab382ceb65cca2252f07..41d599822d3ad5123486dc5c8116be5f99dfd043 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Go 1 Release Notes -->
+<!--{
+       "Title": "Go 1 Release Notes"
+}-->
 {{donotedit}}
 
 <h2 id="introduction">Introduction to Go 1</h2>
index e68b4e285574d507115e74e56cf7f69020157f18..6a8199fd94f1d3a6a7e8ac539645a3fd35b95dec 100644 (file)
@@ -1,4 +1,6 @@
-<!-- FAQ -->
+<!--{
+       "Title": "FAQ"
+}-->
 
 <h2 id="Origins">Origins</h2>
 
index 7168f1d057d130135d80f70ca0643227f680aa38..7edb88013f5455791659e06f2de77f8933506f94 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Go For C++ Programmers -->
+<!--{
+       "Title": "Go For C++ Programmers"
+}-->
 
 <p>
 Go is a systems programming language intended to be a general-purpose
index 45ee0f53f2b04582ffb7a80e109b2a80d1f2a369..2e341771fb421c0447529f29d972b56022aebe63 100644 (file)
@@ -1,5 +1,7 @@
-<!-- The Go Memory Model -->
-<!-- subtitle Version of June 10, 2011 -->
+<!--{
+       "Title": "The Go Memory Model",
+       "Subtitle": "Version of June 10, 2011"
+}-->
 
 <style>
 p.rule {
index 90be6d587b284a1f3e8e3c3ff202fce3f7583db2..8bf6c2195a21fc9e347d242300b5cf1df4b94a8e 100644 (file)
@@ -1,5 +1,7 @@
-<!-- title The Go Programming Language Specification -->
-<!-- subtitle Version of January 13, 2012 -->
+<!--{
+       "Title": "The Go Programming Language Specification",
+       "Subtitle": "Version of January 13, 2012"
+}-->
 
 <!--
 TODO
index 071ca1aa9d7272f2144af884caf3b6e8fe1d5fad..eaa989a200394dadeb8bbf69f1db20dab61fddbf 100644 (file)
@@ -1,4 +1,6 @@
-<!-- A Tutorial for the Go Programming Language -->
+<!--{
+       "Title": "A Tutorial for the Go Programming Language"
+}-->
 <!--
   DO NOT EDIT: created by
     tmpltohtml go_tutorial.tmpl
index ff3057b6efb11d2fac1ec511b0fbf4e7da54fa9c..bde724ce312e4438e89dc854659cd06d0b76d9fe 100644 (file)
@@ -1,4 +1,6 @@
-<!-- A Tutorial for the Go Programming Language -->
+<!--{
+       "Title": "A Tutorial for the Go Programming Language"
+}-->
 {{donotedit}}
 
 <h2>Introduction</h2>
index 70dfe8128979771ea136f6e7928ea6c7fe7869b9..61a90dad126f2bb9a5e025f3a8d5e679cf6fc704 100644 (file)
@@ -1,4 +1,6 @@
-<!-- Getting Started -->
+<!--{
+       "Title": "Getting Started"
+}-->
 
 <h2 id="introduction">Introduction</h2>
 
index 01d3adc9cdda0da30a8a466067f645a933c4f767..746b29c869b3e884a75da3a56eadb9d320eace9d 100644 (file)
@@ -1,4 +1,6 @@
-<!-- About the Go Playground -->
+<!--{
+       "Title": "About the Go Playground"
+}-->
 
 <div class="left-column">
 <p>
index 92273ceaedc5433db31ad02517a41cbed05f44f2..61a3142ff622c75a18ac293b86f3476d140c0a56 100644 (file)
@@ -6,6 +6,7 @@ package main
 
 import (
        "bytes"
+       "encoding/json"
        "flag"
        "fmt"
        "go/ast"
@@ -692,17 +693,14 @@ func serveText(w http.ResponseWriter, text []byte) {
 // Files
 
 var (
-       titleRx        = regexp.MustCompile(`<!-- title ([^\-]*)-->`)
-       subtitleRx     = regexp.MustCompile(`<!-- subtitle ([^\-]*)-->`)
-       firstCommentRx = regexp.MustCompile(`<!--([^\-]*)-->`)
+       doctype   = []byte("<!DOCTYPE ")
+       jsonStart = []byte("<!--{")
+       jsonEnd   = []byte("}-->")
 )
 
-func extractString(src []byte, rx *regexp.Regexp) (s string) {
-       m := rx.FindSubmatch(src)
-       if m != nil {
-               s = strings.TrimSpace(string(m[1]))
-       }
-       return
+type Metadata struct {
+       Title    string
+       Subtitle string
 }
 
 func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath string) {
@@ -716,11 +714,23 @@ func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath strin
 
        // if it begins with "<!DOCTYPE " assume it is standalone
        // html that doesn't need the template wrapping.
-       if bytes.HasPrefix(src, []byte("<!DOCTYPE ")) {
+       if bytes.HasPrefix(src, doctype) {
                w.Write(src)
                return
        }
 
+       // if it begins with a JSON blob, read in the metadata.
+       var meta Metadata
+       if bytes.HasPrefix(src, jsonStart) {
+               if end := bytes.Index(src, jsonEnd); end > -1 {
+                       b := src[len(jsonStart)-1 : end+1] // drop leading <!-- and include trailing }
+                       if err := json.Unmarshal(b, &meta); err != nil {
+                               log.Printf("decoding metadata for %s: %v", relpath, err)
+                       }
+                       src = src[end+len(jsonEnd):]
+               }
+       }
+
        // if it's the language spec, add tags to EBNF productions
        if strings.HasSuffix(abspath, "go_spec.html") {
                var buf bytes.Buffer
@@ -728,15 +738,7 @@ func serveHTMLDoc(w http.ResponseWriter, r *http.Request, abspath, relpath strin
                src = buf.Bytes()
        }
 
-       // get title and subtitle, if any
-       title := extractString(src, titleRx)
-       if title == "" {
-               // no title found; try first comment for backward-compatibility
-               title = extractString(src, firstCommentRx)
-       }
-       subtitle := extractString(src, subtitleRx)
-
-       servePage(w, title, subtitle, "", src)
+       servePage(w, meta.Title, meta.Subtitle, "", src)
 }
 
 func applyTemplate(t *template.Template, name string, data interface{}) []byte {