From 3e1c06bad7c1aa87a5865d6a749e6cd8e2607a8a Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Thu, 3 Aug 2023 19:19:07 -0600 Subject: [PATCH] Build with make --- .gitignore | 1 + Makefile | 11 ++++++++ edit-chapters.go => edit-chapter.go | 40 ++++++++++++++--------------- makepdf.sh | 8 ------ 4 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 Makefile rename edit-chapters.go => edit-chapter.go (77%) delete mode 100644 makepdf.sh diff --git a/.gitignore b/.gitignore index 8a5fe7e..b3d797d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ book.pdf +book.tex diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf4dc9f --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +chapters := 01 02 03 04 05 06 07 08 09 10 11 12 appendix +edited_chapters := $(foreach chapter,$(chapters),edited/$(chapter).markdown) + +book.pdf: $(edited_chapters) title.txt Makefile + pandoc -o book.pdf title.txt $(edited_chapters) --table-of-contents --number-sections -V documentclass=book + +edited/%.markdown: original/%.markdown edit-chapter.go Makefile + go run edit-chapter.go $< $@ + +book.tex: $(edited_chapters) title.txt Makefile + pandoc -o book.tex title.txt $(edited_chapters) --table-of-contents --number-sections -V documentclass=book diff --git a/edit-chapters.go b/edit-chapter.go similarity index 77% rename from edit-chapters.go rename to edit-chapter.go index a234655..48b53ad 100644 --- a/edit-chapters.go +++ b/edit-chapter.go @@ -4,9 +4,8 @@ package main import ( "bytes" - "log" + "fmt" "os" - "path/filepath" "regexp" ) @@ -74,34 +73,35 @@ func replaceFootnote(src []byte) []byte { } func run() error { - paths, err := filepath.Glob("original/*.markdown") + if len(os.Args) != 3 { + fmt.Fprintf(os.Stderr, "usage: %s \n", os.Args[0]) + os.Exit(64) // command line usage error + } + inpath := os.Args[1] + outpath := os.Args[2] + + text, err := os.ReadFile(inpath) if err != nil { return err } - for _, path := range paths { - b, err := os.ReadFile(path) - if err != nil { - return err - } - var edited = b - for _, r := range replacers { - edited = r.re.ReplaceAll(edited, []byte(r.replacement)) - } - edited = footnoteRE.ReplaceAllFunc(edited, replaceFootnote) - - outpath := filepath.Join("edited", filepath.Base(path)) - err = os.WriteFile(outpath, edited, 0666) - if err != nil { - return err - } + for _, r := range replacers { + text = r.re.ReplaceAll(text, []byte(r.replacement)) } + text = footnoteRE.ReplaceAllFunc(text, replaceFootnote) + + err = os.WriteFile(outpath, text, 0666) + if err != nil { + return err + } + return nil } func main() { err := run() if err != nil { - log.Fatal(err) + fmt.Fprintln(os.Stderr, err) + os.Exit(1) } } diff --git a/makepdf.sh b/makepdf.sh deleted file mode 100644 index e0ee762..0000000 --- a/makepdf.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -pandoc -o book.pdf title.txt \ - edited/{01,02,03,04,05,06,07,08,09,10,11,12}.markdown \ - edited/appendix.markdown \ - --table-of-contents \ - --number-sections \ - -V documentclass=book