From 84eeba25620390256a381fa34a6884b6041bbbf6 Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Tue, 28 Jun 2011 10:58:37 -0600 Subject: [PATCH] example markdown binary: try to guess a title --- example/main.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/example/main.go b/example/main.go index 8ff7955..7d2e54b 100644 --- a/example/main.go +++ b/example/main.go @@ -16,6 +16,7 @@ package main import ( + "bytes" "flag" "fmt" "io/ioutil" @@ -46,7 +47,7 @@ func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage:\n"+ " %s [options] [inputfile [outputfile]]\n\n"+ - "Options:\n", os.Args[0]) + "Options:\n",os.Args[0]) flag.PrintDefaults() } flag.Parse() @@ -135,6 +136,19 @@ func main() { } if page { + // if it starts with an

, make that the title + title := "" + if bytes.HasPrefix(output, []byte("

")) { + end := 0 + // we know the buffer ends with a newline, so no need to check bounds + for output[end] != '\n' { + end++ + } + if bytes.HasSuffix(output[:end], []byte("

")) { + title = string(output[len("

") : end-len("

")]) + } + } + ending := "" if xhtml { fmt.Fprint(out, "") } fmt.Fprintln(out, "") - fmt.Fprintln(out, " ") + fmt.Fprintf(out, " %s\n", title) fmt.Fprintf(out, " \n", ending) fmt.Fprintf(out, " \n", ending) if css != "" {