From aee0b312a664dfef96ef717e4e1cdd6de6a915ad Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Mon, 17 Nov 2014 23:36:07 -0800 Subject: [PATCH] Add MarkdownOptionsHandlerFunc in a separate experimental package. It allows enabling automatic Table of Contents widget, which is experimentally provided via a blank import of a handler. Many issues to resolve and need to continue to look for a good solution. But this works for now and is needed for Conception-go to compile as is. --- u/u1/main.go | 6 ++--- u/u10/main.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 u/u10/main.go diff --git a/u/u1/main.go b/u/u1/main.go index 590abe6..9bc86e5 100644 --- a/u/u1/main.go +++ b/u/u1/main.go @@ -10,16 +10,14 @@ import ( // Convert GitHub Flavored Markdown to full HTML page and write to w. func WriteGitHubFlavoredMarkdownViaLocal(w io.Writer, markdown []byte) { - // TODO: Don't hotlink the css file from github.com, serve it locally (it's needed for the GFM html to appear properly) - io.WriteString(w, `
`) + io.WriteString(w, `
`) w.Write(github_flavored_markdown.Markdown(markdown)) io.WriteString(w, `
`) } // TODO: Remove once local version gives matching results. func WriteGitHubFlavoredMarkdownViaGitHub(w io.Writer, markdown []byte) { - // TODO: Don't hotlink the css file from github.com, serve it locally (it's needed for the GFM html to appear properly) - io.WriteString(w, `
`) + io.WriteString(w, `
`) // Convert GitHub-Flavored-Markdown to HTML (includes syntax highlighting for diff, Go, etc.) resp, err := http.Post("https://api.github.com/markdown/raw", "text/x-markdown", bytes.NewReader(markdown)) diff --git a/u/u10/main.go b/u/u10/main.go new file mode 100644 index 0000000..e86e338 --- /dev/null +++ b/u/u10/main.go @@ -0,0 +1,73 @@ +package u10 + +import ( + "fmt" + "io" + "net/http" + "time" + + "github.com/shurcooL/go/github_flavored_markdown" + "github.com/shurcooL/go/u/u1" + + // An experiment in making the frontend resources available. + // This tries to ensure "/table-of-contents.go.js" and "/table-of-contents.css" will be available... + // TODO: This is not quite done and requires figuring out a good way to solve the challenge... + // Relative paths do not work at all when it's a library rather than package main. + // TODO: Perhaps the strings `` and + // `` should be coming + // from the TOC handler package? + // TODO: Perhaps I could use "/go/import/path" notation to ensure no path collisions? + _ "github.com/shurcooL/frontend/table-of-contents/handler" +) + +type Options struct { + TableOfContents bool +} + +// MarkdownOptionsHandlerFunc is an http.Handler that serves rendered Markdown. +type MarkdownOptionsHandlerFunc func(req *http.Request) (markdown []byte, opt *Options, err error) + +func (this MarkdownOptionsHandlerFunc) ServeHTTP(w http.ResponseWriter, req *http.Request) { + markdown, opt, err := this(req) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + if opt == nil { + opt = &Options{} + } + + if _, plain := req.URL.Query()["plain"]; plain { + w.Header().Set("Content-Type", "text/plain") + w.Write(markdown) + } else if _, github := req.URL.Query()["github"]; github { + w.Header().Set("Content-Type", "text/html") + started := time.Now() + switch opt.TableOfContents { + case false: + u1.WriteGitHubFlavoredMarkdownViaGitHub(w, markdown) + case true: + http.Error(w, "not implemented", 500) + panic("not implemented") + } + fmt.Println("rendered GFM via GitHub, took", time.Since(started)) + } else { + w.Header().Set("Content-Type", "text/html") + started := time.Now() + switch opt.TableOfContents { + case false: + u1.WriteGitHubFlavoredMarkdownViaLocal(w, markdown) + case true: + writeGitHubFlavoredMarkdownViaLocalWithToc(w, markdown) + } + fmt.Println("rendered GFM locally, took", time.Since(started)) + } +} + +// writeGitHubFlavoredMarkdownViaLocalWithToc renders GFM as a full HTML page with table of contents and writes to w. +func writeGitHubFlavoredMarkdownViaLocalWithToc(w io.Writer, markdown []byte) { + io.WriteString(w, `
`) + w.Write(github_flavored_markdown.Markdown(markdown)) + io.WriteString(w, `
`) +}