|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "html/template" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/erikgeiser/promptkit/selection" |
| 10 | + "github.com/erikgeiser/promptkit/textinput" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var articleGenerateCmd = &cobra.Command{ |
| 15 | + Use: "generate", |
| 16 | + Short: "Generate article", |
| 17 | + Args: cobra.ExactArgs(1), |
| 18 | + RunE: executeArticleGenerate, |
| 19 | +} |
| 20 | + |
| 21 | +func executeArticleGenerate(_ *cobra.Command, args []string) error { |
| 22 | + data, err := readDataFromInput() |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + |
| 27 | + if err := generate(data, args[0]); err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +func readDataFromInput() (*Data, error) { |
| 35 | + titleInput := textinput.New("Choose the best article title:") |
| 36 | + titleInput.Placeholder = "Think hard!" |
| 37 | + |
| 38 | + title, err := titleInput.RunPrompt() |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + subtitleInput := textinput.New("And well supporting subtitle:") |
| 44 | + subtitleInput.Placeholder = "Think harder!" |
| 45 | + |
| 46 | + subtitle, err := subtitleInput.RunPrompt() |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + authorInput := textinput.New("Who is the author of this masterpiece?") |
| 52 | + authorInput.Placeholder = "Look at yourself in the mirror" |
| 53 | + authorInput. InitialValue = "Ильдар Карымов <[email protected]>, Алексей Ким <[email protected]>" |
| 54 | + |
| 55 | + author, err := authorInput.RunPrompt() |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + languageSelection := selection.New( |
| 61 | + "What language is this article written in?", |
| 62 | + []*selection.Choice{ |
| 63 | + selection.NewChoice("ru"), |
| 64 | + selection.NewChoice("en"), |
| 65 | + }, |
| 66 | + ) |
| 67 | + languageSelection.Filter = nil |
| 68 | + |
| 69 | + language, err := languageSelection.RunPrompt() |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + documentClassSelection := selection.New( |
| 75 | + "What document class should be used?", |
| 76 | + []*selection.Choice{ |
| 77 | + selection.NewChoice("report"), |
| 78 | + selection.NewChoice("article"), |
| 79 | + selection.NewChoice("book"), |
| 80 | + }, |
| 81 | + ) |
| 82 | + documentClassSelection.Filter = nil |
| 83 | + |
| 84 | + documentClass, err := documentClassSelection.RunPrompt() |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + paperSizeSelection := selection.New( |
| 90 | + "What paper size should be used?", |
| 91 | + []*selection.Choice{ |
| 92 | + selection.NewChoice("a4"), |
| 93 | + selection.NewChoice("letter"), |
| 94 | + }, |
| 95 | + ) |
| 96 | + paperSizeSelection.Filter = nil |
| 97 | + |
| 98 | + paperSize, err := paperSizeSelection.RunPrompt() |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + lineStretchInput := textinput.New("What line stretch should be used?") |
| 104 | + lineStretchInput.InitialValue = "1.5" |
| 105 | + |
| 106 | + lineStretchStr, err := lineStretchInput.RunPrompt() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + lineStretch, err := strconv.ParseFloat(lineStretchStr, 64) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + mainFontInput := textinput.New("What main font should be used?") |
| 117 | + mainFontInput.InitialValue = "Times New Roman" |
| 118 | + |
| 119 | + mainFont, err := mainFontInput.RunPrompt() |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + monoFontInput := textinput.New("What monospace font should be used?") |
| 125 | + monoFontInput.InitialValue = "Fira Code" |
| 126 | + |
| 127 | + monoFont, err := monoFontInput.RunPrompt() |
| 128 | + if err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + return &Data{ |
| 133 | + Title: title, |
| 134 | + Subtitle: subtitle, |
| 135 | + Author: author, |
| 136 | + Language: language.String, |
| 137 | + DocumentClass: documentClass.String, |
| 138 | + PaperSize: paperSize.String, |
| 139 | + LineStretch: lineStretch, |
| 140 | + MainFont: mainFont, |
| 141 | + MonoFont: monoFont, |
| 142 | + }, nil |
| 143 | +} |
| 144 | + |
| 145 | +//go:embed resources/article.tpl.md |
| 146 | +var tpl string |
| 147 | + |
| 148 | +func generate(data *Data, outputFileName string) error { |
| 149 | + tpl, err := template.New("article").Parse(tpl) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + outFile, err := os.Create(outputFileName) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + defer outFile.Close() |
| 159 | + |
| 160 | + return tpl.Execute(outFile, data) |
| 161 | +} |
| 162 | + |
| 163 | +type Data struct { |
| 164 | + Title string |
| 165 | + Subtitle string |
| 166 | + Author string |
| 167 | + Language string |
| 168 | + DocumentClass string |
| 169 | + PaperSize string |
| 170 | + LineStretch float64 |
| 171 | + MainFont string |
| 172 | + MonoFont string |
| 173 | +} |
0 commit comments