Skip to content

Instantly share code, notes, and snippets.

@Merovius
Created February 25, 2018 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Merovius/c5b27893f622acf606c3c86c5fd31bf9 to your computer and use it in GitHub Desktop.
Save Merovius/c5b27893f622acf606c3c86c5fd31bf9 to your computer and use it in GitHub Desktop.
tiny tool to extract codeblocks from markdown
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
func main() {
lang := flag.String("lang", "go", "The language tag to grep for.")
flag.Parse()
type blockState int
const (
findBlock blockState = iota
inBlock
skipBlock
)
state := findBlock
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
l := s.Text()
if strings.HasPrefix(l, "```") {
if state == findBlock && *lang != "" && l[3:] != *lang {
state = skipBlock
} else if state == findBlock {
state = inBlock
} else {
state = findBlock
}
continue
}
if state == inBlock {
fmt.Println(l)
}
}
if err := s.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment