Skip to content
Permalink
Browse files

First draft of program

  • Loading branch information...
Depado committed May 18, 2018
1 parent b2c406a commit e41fa0f8b911af5a4c147912b631f4e2a75b9096
Showing with 491 additions and 1 deletion.
  1. +4 −1 .gitignore
  2. +99 −0 Gopkg.lock
  3. +42 −0 Gopkg.toml
  4. +73 −0 main.go
  5. +32 −0 templates/index.tmpl
  6. +241 −0 templates/temp.css
@@ -13,4 +13,7 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

vendor/
chromarkdown
in.md
out.html

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,42 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/Depado/bfchroma"
version = "1.1.0"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.5"

[[constraint]]
name = "gopkg.in/russross/blackfriday.v2"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
73 main.go
@@ -0,0 +1,73 @@
package main

import (
"bytes"
"html/template"
"io/ioutil"
"os"

"github.com/Depado/bfchroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/sirupsen/logrus"
bf "gopkg.in/russross/blackfriday.v2"
)

var exts = bf.NoIntraEmphasis | bf.Tables | bf.FencedCode | bf.Autolink |
bf.Strikethrough | bf.SpaceHeadings | bf.BackslashLineBreak |
bf.DefinitionLists | bf.Footnotes

var flags = bf.UseXHTML | bf.Smartypants | bf.SmartypantsFractions |
bf.SmartypantsDashes | bf.SmartypantsLatexDashes | bf.TOC

// GlobCSS is a byte slice containing the style CSS of the renderer
var GlobCSS template.CSS

func render(input []byte) []byte {
r := bfchroma.NewRenderer(
bfchroma.WithoutAutodetect(),
bfchroma.Extend(
bf.NewHTMLRenderer(bf.HTMLRendererParameters{Flags: flags}),
),
bfchroma.Style("monokai"),
bfchroma.ChromaOptions(html.WithClasses()),
)
if GlobCSS == "" && r.Formatter.Classes {
b := new(bytes.Buffer)
if err := r.Formatter.WriteCSS(b, r.Style); err != nil {
logrus.WithError(err).Warning("Couldn't write CSS")
}
GlobCSS = template.CSS(b.String())
}
return bf.Run(
input,
bf.WithRenderer(r),
bf.WithExtensions(exts),
)
}

func main() {
var err error
var fd *os.File
var t *template.Template
var in []byte

if fd, err = os.Create("out.html"); err != nil {
logrus.WithError(err).Fatal("Couldn't create file")
}
defer fd.Close()

if in, err = ioutil.ReadFile("in.md"); err != nil {
logrus.WithError(err).Fatal("Couldn't read in.md")
}
if t, err = template.ParseFiles("templates/index.tmpl"); err != nil {
logrus.WithError(err).Fatal("Couldn't parse template")
}
err = t.ExecuteTemplate(fd, "index.tmpl", map[string]interface{}{
"title": "Output",
"rendered": template.HTML(string(render(in))),
"css": GlobCSS,
})
if err != nil {
logrus.WithError(err).Fatal("Couldn't execute template")
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit e41fa0f

Please sign in to comment.
You can’t perform that action at this time.