OSDN Git Service

[update] : タイトル書き換えを追加
[alterlinux/hayao.fascode.net.git] / blog / new-post.v
1 import os
2 import ui
3 import time
4 import strconv as strc
5 import regex
6 //import eventbus
7
8 const (
9         win_width  = 300
10         win_height = 180
11 )
12
13 struct App {
14         mut:
15                 window       &ui.Window = 0
16                 first_ipsum  string
17                 second_ipsum string
18                 full_name    string
19 }
20
21 fn main(){
22         // Check environment
23         check_system_env()
24
25         mut app := &App{}
26         app.window = ui.window(
27                 width: win_width
28                 height: win_height
29                 mode: .resizable
30                 //resizable: false
31                 on_resize: fn (width int, height int, win &ui.Window){
32                         // ウィンドウリサイズ時の処理
33                 }
34                 title: "ブログの新規記事の作成"
35                 state: app
36                 children: [
37                         ui.column(
38                                 id: "main"
39                                 width: int(ui.stretch)
40                                 //height: int(ui.stretch)
41                                 alignment: .center
42                                 //spacing: 2
43                                 margin_: 5
44                                 children: [
45                                         ui.row(
46                                                 //height: 20
47                                                 id: "group_url"
48                                                 widths: ui.stretch
49                                                 //heights: ui.stretch
50                                                 children: [
51                                                         ui.label(
52                                                                 id: "url_label"
53                                                                 text: "Please enter URL"
54                                                         )
55                                                         ui.textbox(
56                                                                         id: "url_tb"
57                                                                         read_only: false
58                                                                         //min: 1
59                                                                         max_len: 20
60                                                                         width: 250
61                                                         )
62                                                 ]
63                                         )
64                                         ui.row(
65                                                 //height: 10
66                                                 id: "group_title"
67                                                 widths: ui.stretch
68                                                 heights: ui.stretch
69                                                 children: [
70                                                         ui.label(
71                                                                 id: "title_label"
72                                                                 text: "Please enter the title"
73                                                         )
74                                                         ui.textbox(
75                                                                 id: "title_tb"
76                                                                 read_only: false
77                                                                 max_len: 20
78                                                                 width: 250
79                                                         )
80                                                 ]
81                                         )
82                                         ui.column(
83                                                 id: "group_create"
84                                                 widths: ui.stretch
85                                                 heights: ui.stretch
86                                                 children: [
87                                                         ui.row(
88                                                                 alignment: .center
89                                                                 widths: ui.stretch
90                                                                 margin : ui.Margin{
91                                                                         bottom: 10
92                                                                 }
93                                                                 children: [
94                                                                         ui.button(
95                                                                                 id: "exit_btn"
96                                                                                 text: "Exit"
97                                                                                 radius: .0
98                                                                                 onclick: fn(w voidptr, b &ui.Button){
99                                                                                         exit(0)
100                                                                                 }
101                                                                         )
102                                                                         ui.button(
103                                                                                 id: 'create_btn'
104                                                                                 text: 'Create'
105                                                                                 onclick: fn(_ voidptr, b &ui.Button){
106                                                                                         mut title := b.ui.window.textbox("title_tb").text_
107                                                                                         mut url   := b.ui.window.textbox("url_tb").text_
108                                                                                         //println(text)
109                                                                                         create_article(url, title)
110                                                                                 }
111                                                                         )
112                                                                 ]
113                                                         )
114                                                 ]
115                                         )
116                                 ]
117                         )
118                 ]
119         )
120         ui.run(app.window)
121 }
122
123 fn create_article(url string, title string){
124         //eprintln("まだ実装されてないンゴ")
125         mut now := time.now()
126         mut date := "${strc.v_sprintf("%02d", now.year)}${strc.v_sprintf("%02d", now.month)}${strc.v_sprintf("%02d", now.day)}"
127         mut filename := "posts/$date/$url/index.md"
128         mut command := ["hugo", "new", "\"$filename\""]
129
130         if url == ""{
131                 ui.message_box("Empty URL!")
132                 return
133         }
134
135         mut result := os.execute(command.join(" "))
136         
137         if result.exit_code !=0 {
138                 ui.message_box(result.output)
139         }
140
141         mut re := regex.regex_opt('^title: ".*"$') or {return}
142         mut path := os.resource_abs_path("./src/content/${filename}")
143         println(path)
144         mut article_md := os.read_lines(path) or {
145                 ui.message_box("Failed to open \"$path\"")
146                 return
147                 //exit(1)
148         }
149
150         for cnt,line in article_md{
151                 if re.matches_string(line){
152                         eprintln("書き換え対象を${cnt}行目に発見")
153                         article_md[cnt] = re.replace(line, 'title: "$title"')
154                         eprintln("${line} ==> title: \"$title\"")
155                         break
156                 }
157         }
158
159         os.write_file(path, article_md.join("\n")) or {
160                 ui.message_box("Failed to write to $path")
161         }
162
163         exit(0)
164 }
165
166 fn cmd_available(c string) bool{
167         mut cmd := ""
168         $if windows{
169                 cmd = "WHERE"
170         }$else{
171                 cmd = "type"
172         }
173
174         result := os.execute("${cmd} \"${c}\"")
175         return result.exit_code == 0
176 }
177
178 fn check_system_env(){
179         mut check := 0
180         if ! cmd_available("hugo"){
181                 eprintln("Hugo command was not found on this computer.")
182                 check++
183         }
184
185         if check != 0{
186                 exit(1)
187         }
188         return
189 }