func ProcessMarkdown(text string) string { // GitHub Flavored Markdown-like extensions. 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 //extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK output := blackfriday.Markdown([]byte(text), markdown.NewRenderer(), extensions) return string(output) }
func main() { if len(os.Args) != 4 { fmt.Println("Usage: tickdoc absPath path/to/golang/package output/dir") fmt.Println() fmt.Println("absPath - the absolute path of rendered documentation, used to generate links.") os.Exit(1) } absPath = os.Args[1] dir := os.Args[2] out := os.Args[3] fset := token.NewFileSet() // positions are relative to fset skipTest := func(fi os.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") } pkgs, err := parser.ParseDir(fset, dir, skipTest, parser.ParseComments) if err != nil { log.Fatal(err) } nodes := make(map[string]*Node) for _, pkg := range pkgs { f := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates) ast.Inspect(f, func(n ast.Node) bool { switch decl := n.(type) { case *ast.GenDecl: handleGenDecl(nodes, decl) case *ast.FuncDecl: handleFuncDecl(nodes, decl) } return true }) } ordered := make([]string, 0, len(nodes)) for name, node := range nodes { if name == "" || !ast.IsExported(name) { continue } if node.Embedded { err := node.Embed(nodes) if err != nil { log.Fatal(err) } } else { ordered = append(ordered, name) node.Flatten(nodes) } } sort.Strings(ordered) r := markdown.NewRenderer() for i, name := range ordered { var buf bytes.Buffer n := nodes[name] n.Render(&buf, r, nodes, i) filename := path.Join(out, snaker.CamelToSnake(name)+".md") log.Println("Writing file:", filename, i) f, err := os.Create(filename) if err != nil { log.Fatal(err) } defer f.Close() f.Write(buf.Bytes()) } }
func main() { flag.Usage = usage flag.Parse() args := flag.Args() if len(args) != 2 { flag.Usage() os.Exit(1) } dir := args[0] out := args[1] // Decode config err := decodeConfig(*configPath) if err != nil { log.Fatal(err) } fset := token.NewFileSet() // positions are relative to fset skipTest := func(fi os.FileInfo) bool { return !strings.HasSuffix(fi.Name(), "_test.go") } pkgs, err := parser.ParseDir(fset, dir, skipTest, parser.ParseComments) if err != nil { log.Fatal(err) } nodes := make(map[string]*Node) for _, pkg := range pkgs { f := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates) ast.Inspect(f, func(n ast.Node) bool { switch decl := n.(type) { case *ast.GenDecl: handleGenDecl(nodes, decl) case *ast.FuncDecl: handleFuncDecl(nodes, decl) } return true }) } ordered := make([]string, 0, len(nodes)) for name, node := range nodes { if name == "" || !ast.IsExported(name) || node.Name == "" { continue } if node.Embedded { err := node.Embed(nodes) if err != nil { log.Fatal(err) } } else { ordered = append(ordered, name) node.Flatten(nodes) } } sort.Strings(ordered) r := markdown.NewRenderer(nil) for i, name := range ordered { var buf bytes.Buffer n := nodes[name] weight := (i + 1) * config.IndexWidth if w, ok := config.Weights[name]; ok { weight = w } n.Render(&buf, r, nodes, weight) filename := path.Join(out, snaker.CamelToSnake(name)+".md") log.Println("Writing file:", filename, i) f, err := os.Create(filename) if err != nil { log.Fatal(err) } defer f.Close() f.Write(buf.Bytes()) } }