From e71161efb26bbca57e3d4d33ce271604d1d56a7a Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 9 Oct 2012 19:42:36 -0700 Subject: [PATCH] books with chapters lol --- meta/chapters.txt => examples.txt | 0 meta/introduction.md | 28 ---------- src/execing-processes/execing-processes.go | 2 +- template/{chapter.tmpl => example.tmpl} | 6 +-- template/index.tmpl | 30 +++++++++++ template/site.css | 4 +- tool/generate.go | 62 +++++++++++----------- 7 files changed, 67 insertions(+), 65 deletions(-) rename meta/chapters.txt => examples.txt (100%) delete mode 100644 meta/introduction.md rename template/{chapter.tmpl => example.tmpl} (91%) diff --git a/meta/chapters.txt b/examples.txt similarity index 100% rename from meta/chapters.txt rename to examples.txt diff --git a/meta/introduction.md b/meta/introduction.md deleted file mode 100644 index 678b89d..0000000 --- a/meta/introduction.md +++ /dev/null @@ -1,28 +0,0 @@ -## Introduction - -Go is an open-source programming language designed for -building simple, fast, and reliable software. -It's powerful to use and fun to learn. - -This book is a hands-on introduction to Go. It gives you -the tools you need to understand the why of Go and how to -write real Go programs. - -### Intended Audience - -This book is intended for experienced programmers who may -be new to Go. If you have previous experience with -languages like Ruby, Python, Javascript, C#, or Java, you -should find this book immediately approachable even if -this is your first introduction to Go. Even if you've -worked with Go before, you may find that the material -covers ideas and techniques that you haven't seen -elsewhere. - -### An Example-Based Approach - -Programmers learn to program by writing programs. -_Go by Example_ therefore teaches Go exclusively via -working example programs, with each chapter trying to -provide the programmer a new tool for writing her own -programs. diff --git a/src/execing-processes/execing-processes.go b/src/execing-processes/execing-processes.go index bac35ed..c4e68da 100644 --- a/src/execing-processes/execing-processes.go +++ b/src/execing-processes/execing-processes.go @@ -1,4 +1,4 @@ -// In the previous chapter we looked at spawning external +// In the previous example we looked at spawning external // process. We do this when we need the functionality // of another process accessable to a running Go process. // In other cases we may just want to completely replace diff --git a/template/chapter.tmpl b/template/example.tmpl similarity index 91% rename from template/chapter.tmpl rename to template/example.tmpl index a96a02a..82e7011 100644 --- a/template/chapter.tmpl +++ b/template/example.tmpl @@ -16,7 +16,7 @@ })(); -
+

Go by Example: {{.Name}}

{{range .Segs}} @@ -34,9 +34,9 @@
{{end}} - {{if .NextChapter}} + {{if .NextExample}}

- Next example: {{.NextChapter.Name}}. + Next example: {{.NextExample.Name}}.

{{end}}
+ +## Introduction + +Go is an open-source programming language designed for +building simple, fast, and reliable software. +It's powerful to use and fun to learn. + +This book is a hands-on introduction to Go. It gives you +the tools you need to understand the why of Go and how to +write real Go programs. + +### Intended Audience + +This book is intended for experienced programmers who may +be new to Go. If you have previous experience with +languages like Ruby, Python, Javascript, C#, or Java, you +should find this book immediately approachable even if +this is your first introduction to Go. Even if you've +worked with Go before, you may find that the material +covers ideas and techniques that you haven't seen +elsewhere. + +### An Example-Based Approach + +Programmers learn to program by writing programs. +_Go by Example_ therefore teaches Go exclusively via +working example programs, with each example trying to +provide the programmer a new tool for writing her own +programs. + diff --git a/template/site.css b/template/site.css index 3699267..717656f 100644 --- a/template/site.css +++ b/template/site.css @@ -63,7 +63,7 @@ h2 { h2 a { text-decoration: none; } -div.chapter { +div.example { width: 900px; min-width: 900px; max-width: 900px; @@ -71,7 +71,7 @@ div.chapter { margin-right: auto; margin-bottom: 120px; } -div.chapter table { +div.example table { margin-top: 15px; margin-bottom: 20px; } diff --git a/tool/generate.go b/tool/generate.go index 4b89377..8134919 100644 --- a/tool/generate.go +++ b/tool/generate.go @@ -123,10 +123,10 @@ type Seg struct { CodeEmpty, CodeLeading bool } -type Chapter struct { +type Example struct { Id, Name string Segs [][]*Seg - NextChapter *Chapter + NextExample *Example } func parseSegs(sourcePath string) []*Seg { @@ -190,50 +190,50 @@ func parseAndRenderSegs(sourcePath string) []*Seg { return segs } -func parseChapters() []*Chapter { - chapterNames := readLines("meta/chapters.txt") - chapters := make([]*Chapter, 0) - for _, chapterName := range chapterNames { - if (chapterName != "") && !strings.HasPrefix(chapterName, "#") { - chapter := Chapter{Name: chapterName} - chapterId := strings.ToLower(chapterName) - chapterId = strings.Replace(chapterId, " ", "-", -1) - chapterId = strings.Replace(chapterId, "/", "-", -1) - chapter.Id = chapterId - chapter.Segs = make([][]*Seg, 0) - sourcePaths := mustGlob("src/" + chapterId + "/*") +func parseExamples() []*Example { + exampleNames := readLines("examples.txt") + examples := make([]*Example, 0) + for _, exampleName := range exampleNames { + if (exampleName != "") && !strings.HasPrefix(exampleName, "#") { + example := Example{Name: exampleName} + exampleId := strings.ToLower(exampleName) + exampleId = strings.Replace(exampleId, " ", "-", -1) + exampleId = strings.Replace(exampleId, "/", "-", -1) + example.Id = exampleId + example.Segs = make([][]*Seg, 0) + sourcePaths := mustGlob("src/" + exampleId + "/*") for _, sourcePath := range sourcePaths { sourceSegs := parseAndRenderSegs(sourcePath) - chapter.Segs = append(chapter.Segs, sourceSegs) + example.Segs = append(example.Segs, sourceSegs) } - chapters = append(chapters, &chapter) + examples = append(examples, &example) } } - for i, chapter := range chapters { - if i < (len(chapters) - 1) { - chapter.NextChapter = chapters[i+1] + for i, example := range examples { + if i < (len(examples) - 1) { + example.NextExample = examples[i+1] } } - return chapters + return examples } -func renderIndex(chapters []*Chapter) { +func renderIndex(examples []*Example) { indexTmpl := template.New("index") _, err := indexTmpl.Parse(mustReadFile("template/index.tmpl")) check(err) indexF, err := os.Create(siteDir + "/index.html") check(err) - indexTmpl.Execute(indexF, chapters) + indexTmpl.Execute(indexF, examples) } -func renderChapters(chapters []*Chapter) { - chapterTmpl := template.New("chapter") - _, err := chapterTmpl.Parse(mustReadFile("template/chapter.tmpl")) +func renderExamples(examples []*Example) { + exampleTmpl := template.New("example") + _, err := exampleTmpl.Parse(mustReadFile("template/example.tmpl")) check(err) - for _, chapter := range chapters { - chapterF, err := os.Create(siteDir + "/" + chapter.Id) + for _, example := range examples { + exampleF, err := os.Create(siteDir + "/" + example.Id) check(err) - chapterTmpl.Execute(chapterF, chapter) + exampleTmpl.Execute(exampleF, example) } } @@ -243,7 +243,7 @@ func main() { copyFile("template/site.css", siteDir+"/site.css") copyFile("template/favicon.ico", siteDir+"/favicon.ico") copyFile("template/404.html", siteDir+"/404.html") - chapters := parseChapters() - renderIndex(chapters) - renderChapters(chapters) + examples := parseExamples() + renderIndex(examples) + renderExamples(examples) }