|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | +) |
| 10 | + |
| 11 | +const problemBaseCN = "https://leetcode-cn.com/problems/" |
| 12 | +const problemBase = "https://leetcode.com/problems/" |
| 13 | + |
| 14 | +func createQuestion(basePath string, item *Item, question *Question) error { |
| 15 | + titleSlug := item.Question.TitleSlug |
| 16 | + folder := path.Join(basePath, titleSlug) |
| 17 | + err := createFolder(folder) |
| 18 | + if err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + defaultCode := parseDefaultCode(question.CodeDefinition) |
| 22 | + err = createSolutionFile(folder, defaultCode) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + err = createReadmeFile(folder, item.Title, titleSlug, question.TranslatedContent) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +func createFolder(folder string) error { |
| 34 | + err := os.Mkdir(folder, os.ModePerm) |
| 35 | + if err != nil { |
| 36 | + if os.IsExist(err) { |
| 37 | + fmt.Println("folder existed: ", folder) |
| 38 | + return err |
| 39 | + } |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func createSolutionFile(folder, defaultCode string) error { |
| 45 | + solutionPath := path.Join(folder, "solution.go") |
| 46 | + f, err := os.Create(solutionPath) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("failed to create file: %s\n", solutionPath) |
| 49 | + } |
| 50 | + defer f.Close() |
| 51 | + w := bufio.NewWriter(f) |
| 52 | + fmt.Fprintln(w, "package main") |
| 53 | + fmt.Fprintln(w) |
| 54 | + fmt.Fprint(w, defaultCode) |
| 55 | + return w.Flush() |
| 56 | +} |
| 57 | + |
| 58 | +func createReadmeFile(folder, title, titleSlug, content string) error { |
| 59 | + readmePath := path.Join(folder, "readme.md") |
| 60 | + f, err := os.Create(readmePath) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to create file: %s\n", readmePath) |
| 63 | + } |
| 64 | + defer f.Close() |
| 65 | + w := bufio.NewWriter(f) |
| 66 | + fmt.Fprintln(w, "## "+title) |
| 67 | + fmt.Fprintln(w) |
| 68 | + fmt.Fprint(w, content) |
| 69 | + fmt.Fprintln(w) |
| 70 | + fmt.Fprintln(w,"-----") |
| 71 | + fmt.Fprintln(w) |
| 72 | + fmt.Fprintln(w, "### 链接:") |
| 73 | + fmt.Fprintln(w) |
| 74 | + fmt.Fprintln(w, "中文:"+problemBaseCN+titleSlug) |
| 75 | + fmt.Fprintln(w) |
| 76 | + fmt.Fprintln(w, "英文:"+problemBase+titleSlug) |
| 77 | + return w.Flush() |
| 78 | +} |
| 79 | + |
| 80 | +func parseDefaultCode(codeDeinition string) string { |
| 81 | + var CodeDefinitions []CodeDefinition |
| 82 | + bytes := []byte(codeDeinition) |
| 83 | + err := json.Unmarshal(bytes, &CodeDefinitions) |
| 84 | + if err != nil { |
| 85 | + fmt.Println("Failed to parse code definition json: ", err) |
| 86 | + return "" |
| 87 | + } |
| 88 | + for _, item := range CodeDefinitions { |
| 89 | + if item.Value == "golang" { |
| 90 | + return item.DefaultCode |
| 91 | + } |
| 92 | + } |
| 93 | + return "" |
| 94 | +} |
| 95 | + |
| 96 | +type CodeDefinition struct { |
| 97 | + Value string |
| 98 | + Text string |
| 99 | + DefaultCode string |
| 100 | +} |
0 commit comments