Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for header IDs #4

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ Here is the help message for the tool:
Blackfriday Markdown Processor v1.1
Available at http://github.com/russross/blackfriday

Copyright © 2011 Russ Ross <[email protected]>
Copyright © 2011-2016 Russ Ross <[email protected]> and contributors.
Distributed under the Simplified BSD License
See website for details

@@ -25,6 +25,7 @@ Here is the help message for the tool:
Options:
-cpuprofile="": Write cpu profile to a file
-css="": Link to a CSS stylesheet (implies -page)
-embed=false: Embed CSS stylesheet instead of linking (requires -css)
-fractions=true: Use improved fraction rules for smartypants
-latex=false: Generate LaTeX output instead of HTML
-latexdashes=true: Use LaTeX-style dash rules for smartypants
@@ -41,7 +42,7 @@ License

Blackfriday is distributed under the Simplified BSD License:

> Copyright © 2011 Russ Ross. All rights reserved.
> Copyright © 2011-2016 Russ Ross and contributors. All rights reserved.
>
> Redistribution and use in source and binary forms, with or without modification, are
> permitted provided that the following conditions are met:
48 changes: 42 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
// Blackfriday Markdown Processor
// Available at http://github.com/russross/blackfriday
//
// Copyright © 2011 Russ Ross <[email protected]>.
// Copyright © 2011-2016 Russ Ross <[email protected]> and contributors.
// Distributed under the Simplified BSD License.
// See README.md for details.
//
@@ -18,26 +18,33 @@ package main
import (
"flag"
"fmt"
"github.com/russross/blackfriday"
"io/ioutil"
"os"
"runtime/pprof"
"strings"

"github.com/TomOnTime/markdownutils"
"github.com/russross/blackfriday"
"github.com/shurcooL/sanitized_anchor_name"
)

const DEFAULT_TITLE = ""

func main() {
// parse command-line options
var page, toc, toconly, xhtml, latex, smartypants, latexdashes, fractions bool
var css, cpuprofile string
var page, toc, toconly, mdtoc, xhtml, latex, smartypants, latexdashes, fractions, embed bool
var css, cpuprofile, anchorstyle string
var repeat int
flag.BoolVar(&page, "page", false,
"Generate a standalone HTML page (implies -latex=false)")
flag.BoolVar(&toc, "toc", false,
"Generate a table of contents (implies -latex=false)")
flag.BoolVar(&toconly, "toconly", false,
"Generate a table of contents only (implies -toc)")
flag.BoolVar(&mdtoc, "mdtoc", false,
"Generate a Markdown table of contents only (implies -toc -toconly -latex=false)")
flag.StringVar(&anchorstyle, "anchorstyle", "gitlab",
"When used with -mdtoc selects compatibility: gitlab, github, legacy")
flag.BoolVar(&xhtml, "xhtml", true,
"Use XHTML-style tags in HTML output")
flag.BoolVar(&latex, "latex", false,
@@ -50,14 +57,16 @@ func main() {
"Use improved fraction rules for smartypants")
flag.StringVar(&css, "css", "",
"Link to a CSS stylesheet (implies -page)")
flag.BoolVar(&css, "embed", true,
"Embed CSS stylesheet instead of linking (requires -css)")
flag.StringVar(&cpuprofile, "cpuprofile", "",
"Write cpu profile to a file")
flag.IntVar(&repeat, "repeat", 1,
"Process the input multiple times (for benchmarking)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Blackfriday Markdown Processor v"+blackfriday.VERSION+
"\nAvailable at http://github.com/russross/blackfriday\n\n"+
"Copyright © 2011 Russ Ross <[email protected]>\n"+
"Copyright © 2011-2015 Russ Ross <[email protected]> and contributors\n"+
"Distributed under the Simplified BSD License\n"+
"See website for details\n\n"+
"Usage:\n"+
@@ -81,6 +90,11 @@ func main() {
if toc {
latex = false
}
if mdtoc {
toc = true
toconly = true
latex = false
}

// turn on profiling?
if cpuprofile != "" {
@@ -120,6 +134,8 @@ func main() {
extensions |= blackfriday.EXTENSION_AUTOLINK
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
extensions |= blackfriday.EXTENSION_SPACE_HEADERS
extensions |= blackfriday.EXTENSION_HEADER_IDS
extensions |= blackfriday.EXTENSION_FOOTNOTES

var renderer blackfriday.Renderer
if latex {
@@ -145,13 +161,33 @@ func main() {
htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
title = getTitle(input)
}
if mdtoc {
htmlFlags |= blackfriday.HTML_TOC_MD
}
if toconly {
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
}
if toc {
htmlFlags |= blackfriday.HTML_TOC
}
renderer = blackfriday.HtmlRenderer(htmlFlags, title, css)
if embed {
htmlFlags |= blackfriday.HTML_EMBED_CSS
}

// Determine which anchor generator to use.
sanitizeMap := map[string]func(string) string{
"legacy": sanitized_anchor_name.Create,
"github": markdownutils.CreateGitHubAnchor,
"gitlab": markdownutils.CreateGitLabAnchor,
}
sanitize := sanitizeMap[anchorstyle]
if sanitize == nil {
fmt.Fprintln(os.Stderr, "Not a valid -anchorstyle:", anchorstyle)
os.Exit(-1)
}

renderer = blackfriday.HtmlRendererWithParameters(htmlFlags, title, css,
blackfriday.HtmlRendererParameters{SanitizedAnchorNameOverride: sanitize})
}

// parse and render