Usage
go run *go github.com/owulveryck/test/
Usage
go run *go github.com/owulveryck/test/
package main | |
import ( | |
"bytes" | |
"io" | |
"io/ioutil" | |
"github.com/russross/blackfriday" | |
) | |
func renderMD(r io.Reader) (io.ReadWriter, error) { | |
input, err := ioutil.ReadAll(r) | |
if err != nil { | |
return nil, err | |
} | |
// set up options | |
extensions := 0 | |
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS | |
extensions |= blackfriday.EXTENSION_TABLES | |
extensions |= blackfriday.EXTENSION_FENCED_CODE | |
extensions |= blackfriday.EXTENSION_AUTOLINK | |
extensions |= blackfriday.EXTENSION_STRIKETHROUGH | |
extensions |= blackfriday.EXTENSION_SPACE_HEADERS | |
var renderer blackfriday.Renderer | |
// render the data into HTML | |
htmlFlags := 0 | |
htmlFlags |= blackfriday.HTML_USE_XHTML | |
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS | |
htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS | |
htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES | |
title := "" | |
htmlFlags |= blackfriday.HTML_COMPLETE_PAGE | |
//title = getTitle(r) | |
title = "blabla" | |
//htmlFlags |= blackfriday.HTML_OMIT_CONTENTS | |
htmlFlags |= blackfriday.HTML_TOC | |
// renderer = blackfriday.HtmlRenderer(htmlFlags, title, css) | |
renderer = blackfriday.HtmlRenderer(htmlFlags, title, "css") | |
// parse and render | |
var output []byte | |
output = blackfriday.Markdown(input, renderer, extensions) | |
return bytes.NewBuffer(output), nil | |
} |
package main | |
import ( | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
"github.com/gorilla/handlers" | |
"github.com/gorilla/mux" | |
"gopkg.in/src-d/go-billy.v4/memfs" | |
git "gopkg.in/src-d/go-git.v4" | |
"gopkg.in/src-d/go-git.v4/storage/memory" | |
) | |
var ( | |
worktree *git.Worktree | |
githubRepo string | |
) | |
func main() { | |
// Clones the given repository in memory, creating the remote, the local | |
// branches and fetching the objects, exactly as: | |
// Filesystem abstraction based on memory | |
fs := memfs.New() | |
// Git objects storer based on memory | |
storer := memory.NewStorage() | |
githubRepo = os.Args[1] | |
//r, err := git.Clone(storer, fs, &git.CloneOptions{ | |
repo, err := git.Clone(storer, fs, &git.CloneOptions{ | |
URL: "https://" + githubRepo, | |
Depth: 1, | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
worktree, err = repo.Worktree() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Use gorilla/mux for rich routing. | |
// See http://www.gorillatoolkit.org/pkg/mux | |
r := mux.NewRouter() | |
r.PathPrefix("/" + githubRepo).HandlerFunc(githubHandler).Methods("GET") | |
http.Handle("/", handlers.CombinedLoggingHandler(os.Stderr, r)) | |
log.Fatal(http.ListenAndServe(":8081", nil)) | |
} | |
func githubHandler(w http.ResponseWriter, r *http.Request) { | |
worktree.Pull(&git.PullOptions{}) | |
result := strings.Replace(r.URL.Path[1:], githubRepo, "", -1) | |
log.Println(result) | |
resource, err := worktree.Filesystem.Open(result) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
html, err := renderMD(resource) | |
resource.Close() | |
w.WriteHeader(200) | |
if _, err := io.Copy(w, html); err != nil { | |
return | |
} | |
return | |
} |