Created
December 4, 2018 04:51
-
-
Save xchao0213/778658fcc516c1ed80c52722cb6a225d to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"fmt" | |
"log" | |
"os" | |
"bufio" | |
"regexp" | |
"strconv" | |
"path" | |
"strings" | |
) | |
var ( | |
inputFile string | |
fullName string | |
fileExt string | |
fileName string | |
currentFile *os.File | |
) | |
func main(){ | |
// cp /Data/Book/books/vuejs.org/src/v2/api/index.md ./ | |
// fmt.Printf("Please input filename \n") | |
// fmt.Scanln(&Filename) | |
// fmt.Printf("%s in process\n", Filename) | |
inputFile = os.Args[1] | |
fmt.Printf("%s in process\n", inputFile) | |
fullName = path.Base(inputFile) | |
fileExt = path.Ext(fullName) | |
fileName = strings.TrimSuffix(fullName, fileExt) | |
readLine(inputFile,fileName) | |
} | |
func readLine(path string,prefix string){ | |
mdfile, err := os.Open(path) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer mdfile.Close() | |
scanner := bufio.NewScanner(mdfile) | |
scanner.Split(bufio.ScanLines) | |
rege, _ := regexp.Compile(`^#{2}\s`) | |
count := 0 | |
buffer := "" | |
keyword := "" | |
for scanner.Scan() { | |
if rege.MatchString(scanner.Text()) { | |
keyword = keyword + "\n" + scanner.Text() | |
filename := prefix + strconv.Itoa(count) + ".md" | |
// fmt.Println(scanner.Text()) | |
if count ==0 || len(buffer) > 4000 { | |
fmt.Println(count,len(buffer)/1000,"k") | |
filecreate, _ := os.Create(filename) | |
currentFile = filecreate | |
filecreate.WriteString(buffer) | |
buffer = "" | |
count = count + 1 | |
} | |
} | |
buffer = buffer + "\n" + scanner.Text() | |
} | |
if len(buffer) < 4000 { | |
currentFile.WriteString(buffer) | |
} else { | |
lastname := prefix + strconv.Itoa(count) + ".md" | |
lastfile, _ := os.Create(lastname) | |
lastfile.WriteString(buffer) | |
} | |
keyfile, _ := os.Create("keyword.md") | |
keyfile.WriteString(keyword) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment