56 chars will be a bitch
This commit is contained in:
parent
449c0da96b
commit
7087dc38b5
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
// **golit** generates literate-programming style HTML
|
// **golit** generates literate-programming style HTML
|
||||||
// documentation from a Go source files.
|
// documentation from a Go source files.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -27,18 +26,19 @@ func check(err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll implement Markdown rendering and Pygments syntax
|
// We'll implement Markdown rendering and Pygments
|
||||||
// highlighting by piping data through external programs.
|
// syntax highlighting by piping data through external
|
||||||
// This is a general helper for handling both cases.
|
// programs. This is a general helper for handling both
|
||||||
func pipedCmd(binary string, argv []string, input string) string {
|
// cases.
|
||||||
cmd := exec.Command(binary, argv...)
|
func pipe(bin string, args []string, in string) string {
|
||||||
|
cmd := exec.Command(bin, args...)
|
||||||
in, err := cmd.StdinPipe()
|
in, err := cmd.StdinPipe()
|
||||||
check(err)
|
check(err)
|
||||||
out, err := cmd.StdoutPipe()
|
out, err := cmd.StdoutPipe()
|
||||||
check(err)
|
check(err)
|
||||||
err = cmd.Start()
|
err = cmd.Start()
|
||||||
check(err)
|
check(err)
|
||||||
in.Write([]byte(input))
|
in.Write([]byte(in))
|
||||||
check(err)
|
check(err)
|
||||||
err = in.Close()
|
err = in.Close()
|
||||||
check(err)
|
check(err)
|
||||||
@ -49,17 +49,18 @@ func pipedCmd(binary string, argv []string, input string) string {
|
|||||||
return string(bytes)
|
return string(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll break the code into {docs, code} pairs,
|
// We'll break the code into {docs, code} pairs, and
|
||||||
// and then render those text segments before
|
// then render those text segments before including them
|
||||||
// including them in the HTML doc.
|
// in the HTML doc.
|
||||||
type segment struct {
|
type segment struct {
|
||||||
docs, code, docsRendered, codeRendered string
|
docs, code, docsRendered, codeRendered string
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Accept exactly 1 argument - the input filename, less the .go extension.
|
// Accept exactly 1 argument - the input filename,
|
||||||
|
// less the .go extension.
|
||||||
if len(os.Args) != 2 {
|
if len(os.Args) != 2 {
|
||||||
fmt.Fprintln(os.Stderr, "Usage: go run tool/generate.go input > output.html")
|
fmt.Fprintln(os.Stderr, "unexpected args")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,9 +72,9 @@ func main() {
|
|||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
// Read the source file in, split into lines.
|
// Read the source file in, split into lines.
|
||||||
sourceBytes, err := ioutil.ReadFile(os.Args[1]+".go")
|
srcBytes, err := ioutil.ReadFile(os.Args[1]+".go")
|
||||||
check(err)
|
check(err)
|
||||||
lines := strings.Split(string(sourceBytes), "\n")
|
lines := strings.Split(string(srcBytes), "\n")
|
||||||
|
|
||||||
// Group lines into docs/code segments.
|
// Group lines into docs/code segments.
|
||||||
// Special case the header to go in its own segment.
|
// Special case the header to go in its own segment.
|
||||||
@ -111,34 +112,41 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print HTML header.
|
// Print HTML header.
|
||||||
|
title := titlePat.ReplaceAllString(lines[0], "")
|
||||||
fmt.Printf(`
|
fmt.Printf(`
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-eqiv="content-type" content="text/html;charset=utf-8">
|
<meta http-eqiv="content-type"
|
||||||
|
content="text/html;charset=utf-8">
|
||||||
<title>%s</title>
|
<title>%s</title>
|
||||||
<link rel=stylesheet href="http://jashkenas.github.com/docco/resources/docco.css">
|
<link rel=stylesheet href="../style/lit.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="container">
|
<div id="container">
|
||||||
<div id="background"></div>
|
<div id="background"></div>
|
||||||
<table cellspacing="0" cellpadding="0">
|
<table cellspacing="0" cellpadding="0">
|
||||||
<thead>
|
<thead>
|
||||||
<tr><td class=docs></td><td class=code></td></tr>
|
<tr>
|
||||||
|
<td class=docs></td>
|
||||||
|
<td class=code></td>
|
||||||
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>`, titlePat.ReplaceAllString(lines[0], ""))
|
<tbody>`, title)
|
||||||
|
|
||||||
// Print HTML docs/code segments.
|
// Print HTML docs/code segments.
|
||||||
for _, seg := range segments {
|
for _, seg := range segments {
|
||||||
fmt.Printf("<tr><td class=docs>%s</td><td class=code>%s</td></tr>\n", seg.docsRendered, seg.codeRendered)
|
fmt.Printf(
|
||||||
|
`<tr>
|
||||||
|
<td class=docs>%s</td>
|
||||||
|
<td class=code>%s</td>
|
||||||
|
</tr>`, seg.docsRendered, seg.codeRendered)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print HTML footer.
|
// Print HTML footer.
|
||||||
fmt.Print(`
|
fmt.Print(`</tbody>
|
||||||
</tbody>
|
</table>
|
||||||
</table>
|
</div>
|
||||||
</div>
|
</body>
|
||||||
</body>
|
</html>`)
|
||||||
</html>
|
|
||||||
`)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user