more nuanced code rendering

This commit is contained in:
Mark McGranaghan 2012-10-09 18:56:20 -07:00
parent bc39dbc769
commit f9ef42b960
3 changed files with 32 additions and 23 deletions

View File

@ -18,16 +18,22 @@
<body> <body>
<div class="chapter" id="{{.Id}}"> <div class="chapter" id="{{.Id}}">
<h2>{{.Name}}</h2> <h2>{{.Name}}</h2>
{{range .Segs}}
<table cellspacing="0" cellpadding="0"> <table cellspacing="0" cellpadding="0">
<tbody> <tbody>
{{range .Segs}} {{range .}}
<tr> <tr>
<td class=docs>{{.DocsRendered}}</td> <td class="docs">
<td class="{{.CodeClasses}}">{{.CodeRendered}}</td> {{.DocsRendered}}
</td>
<td class="code{{if .CodeEmpty}} empty{{end}}{{if .CodeLeading}} leading{{end}}">
{{.CodeRendered}}
</td>
</tr> </tr>
{{end}} {{end}}
</tbody> </tbody>
</table> </table>
{{end}}
</div> </div>
</body> </body>
</html> </html>

View File

@ -68,6 +68,9 @@ div.chapter {
margin-right: auto; margin-right: auto;
margin-bottom: 120px; margin-bottom: 120px;
} }
div.chapter table {
margin-bottom: 20px;
}
div#intro { div#intro {
width: 420px; width: 420px;
min-width: 420px; min-width: 420px;
@ -102,12 +105,14 @@ td.code {
padding-top: 5px; padding-top: 5px;
padding-right: 5px; padding-right: 5px;
padding-left: 5px; padding-left: 5px;
padding-bottom: 11px; padding-bottom: 5px;
vertical-align: top; vertical-align: top;
background: #f0f0f0; background: #f0f0f0;
} }
td.code.leading {
padding-bottom: 11px;
}
td.code.empty { td.code.empty {
border-left: none;
background: #ffffff; background: #ffffff;
} }
pre, code { pre, code {

View File

@ -119,12 +119,13 @@ var todoPat = regexp.MustCompile("\\/\\/ todo: ")
type Seg struct { type Seg struct {
Docs, DocsRendered string Docs, DocsRendered string
Code, CodeRendered, CodeClasses string Code, CodeRendered string
CodeEmpty, CodeLeading bool
} }
type Chapter struct { type Chapter struct {
Id, Name string Id, Name string
Segs []*Seg Segs [][]*Seg
} }
func parseSegs(sourcePath string) []*Seg { func parseSegs(sourcePath string) []*Seg {
@ -167,7 +168,11 @@ func parseSegs(sourcePath string) []*Seg {
lastSeen = "code" lastSeen = "code"
} }
} }
return append(segs, &Seg{Code: "", Docs: ""}) for i, seg := range segs {
seg.CodeEmpty = (seg.Code == "")
seg.CodeLeading = (i < (len(segs) - 1))
}
return segs
} }
func parseAndRenderSegs(sourcePath string) []*Seg { func parseAndRenderSegs(sourcePath string) []*Seg {
@ -194,13 +199,12 @@ func parseChapters() []*Chapter {
chapterId = strings.Replace(chapterId, " ", "-", -1) chapterId = strings.Replace(chapterId, " ", "-", -1)
chapterId = strings.Replace(chapterId, "/", "-", -1) chapterId = strings.Replace(chapterId, "/", "-", -1)
chapter.Id = chapterId chapter.Id = chapterId
chapter.Segs = make([][]*Seg, 0)
sourcePaths := mustGlob("src/" + chapterId + "/*") sourcePaths := mustGlob("src/" + chapterId + "/*")
segs := []*Seg{}
for _, sourcePath := range sourcePaths { for _, sourcePath := range sourcePaths {
sourceSegs := parseAndRenderSegs(sourcePath) sourceSegs := parseAndRenderSegs(sourcePath)
segs = append(segs, sourceSegs...) chapter.Segs = append(chapter.Segs, sourceSegs)
} }
chapter.Segs = segs
chapters = append(chapters, &chapter) chapters = append(chapters, &chapter)
} }
} }
@ -221,12 +225,6 @@ func renderChapters(chapters []*Chapter) {
_, err := chapterTmpl.Parse(mustReadFile("template/chapter.tmpl")) _, err := chapterTmpl.Parse(mustReadFile("template/chapter.tmpl"))
check(err) check(err)
for _, chapter := range chapters { for _, chapter := range chapters {
for _, seg := range chapter.Segs {
seg.CodeClasses = "code"
if seg.Code == "" {
seg.CodeClasses = seg.CodeClasses + " empty"
}
}
chapterF, err := os.Create(siteDir + "/" + chapter.Id) chapterF, err := os.Create(siteDir + "/" + chapter.Id)
check(err) check(err)
chapterTmpl.Execute(chapterF, chapter) chapterTmpl.Execute(chapterF, chapter)