Created
February 25, 2018 16:22
-
-
Save Merovius/c5b27893f622acf606c3c86c5fd31bf9 to your computer and use it in GitHub Desktop.
tiny tool to extract codeblocks from markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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