Skip to content

Instantly share code, notes, and snippets.

@hyuki
Created April 9, 2018 13:42
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 hyuki/7ac6bfceb2c16dc69f2a3df03ea128e2 to your computer and use it in GitHub Desktop.
Save hyuki/7ac6bfceb2c16dc69f2a3df03ea128e2 to your computer and use it in GitHub Desktop.
■をh1に、●をh2に変換するテスト
package main
import (
"bufio"
"fmt"
"os"
"log"
"regexp"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: %s input-filename output-filename\n", os.Args[0])
os.Exit(1)
}
infilename := os.Args[1]
outfilename := os.Args[2]
fmt.Printf("input-filename = %s\n", infilename)
fmt.Printf("output-filename = %s\n", outfilename)
infile, err := os.Open(infilename)
check(err)
outfile, err := os.Create(outfilename)
check(err)
linenumber := 0
scanner := bufio.NewScanner(infile)
h1 := regexp.MustCompile("^■(.*)")
h2 := regexp.MustCompile("^●(.*)")
for scanner.Scan() {
line := scanner.Text()
if s := h1.FindStringSubmatch(line); s != nil {
fmt.Fprintf(outfile, "<h1>%s</h1>\n", s[1])
} else if s := h2.FindStringSubmatch(line); s != nil {
fmt.Fprintf(outfile, "<h2>%s</h2>\n", s[1])
} else {
fmt.Fprintf(outfile, "%s\n", line)
}
linenumber++
}
check(scanner.Err())
check(infile.Close())
check(outfile.Close())
}
■これは見出し
これは見出しではない。
■Hello, World.
●これは小見出し
@hyuki
Copy link
Author

hyuki commented Apr 9, 2018

$ go run a.go input.txt ouput.txt
$ cat output.txt
<h1>これは見出し</h1>
これは見出しではない。

<h1>Hello, World.</h1>
<h2>これは小見出し</h2>

<h1></h1>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment