Skip to content

Commit

Permalink
Add MarkdownOptionsHandlerFunc in a separate experimental package.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmitshur committed Nov 18, 2014
1 parent f5c6dbe commit aee0b31
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
6 changes: 2 additions & 4 deletions u/u1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, `<html><head><meta charset="utf-8"><style>code, div.highlight { tab-size: 4; }</style><link href="https://assets-cdn.github.com/assets/github-aac267329edcf91101f8b8afc0575e8e882615cc.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;">`)
io.WriteString(w, `<html><head><meta charset="utf-8"><link href="https://dl.dropboxusercontent.com/u/8554242/temp/github-flavored-markdown.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;">`)
w.Write(github_flavored_markdown.Markdown(markdown))
io.WriteString(w, `</article></body></html>`)
}

// 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, `<html><head><meta charset="utf-8"><link href="https://assets-cdn.github.com/assets/github-aac267329edcf91101f8b8afc0575e8e882615cc.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;">`)
io.WriteString(w, `<html><head><meta charset="utf-8"><link href="https://dl.dropboxusercontent.com/u/8554242/temp/github-flavored-markdown.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;">`)

// 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))
Expand Down
73 changes: 73 additions & 0 deletions u/u10/main.go
Original file line number Diff line number Diff line change
@@ -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 `<script type="text/javascript" src="/table-of-contents.go.js"></script>` and
// `<link href="/table-of-contents.css" media="all" rel="stylesheet" type="text/css" />` 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, `<html><head><meta charset="utf-8"><link href="https://dl.dropboxusercontent.com/u/8554242/temp/github-flavored-markdown.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /><link href="/table-of-contents.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;">`)
w.Write(github_flavored_markdown.Markdown(markdown))
io.WriteString(w, `</article><script type="text/javascript" src="/table-of-contents.go.js"></script></body></html>`)
}

0 comments on commit aee0b31

Please sign in to comment.