0%

Blog Template

需求

我习惯把在LeetCode上写的代码发到博客上,过程中要做一些琐碎的事情,例如:

先在博客的根目录打开git bash,用hexo命令new一篇新的文章:hexo new “LeetCode-xxx”,用编辑器打开输入分类,题目<!--more-->,结果,代码,复杂度等,如图所示。

于是我想借助java稍微简化下步骤,顺便写个GUI,温习一下我的javaFX。

产品

BETA3.1

新特性

添加了新功能:树代码生成

Snipaste_2020-11-24_11-15-30.png

BETA3.0

新特性

添加了新的功能:括号转换和链表代码生成。

Snipaste_2020-11-20_22-40-04.png

Snipaste_2020-11-20_22-40-33.png

Snipaste_2020-11-20_22-44-33.png

BETA2.0

新特性

  • Gradle也挺好,这次用Gradle。
  • 增加了Dialog界面
  • 用JFoenix稍加美化(按钮,输入框)
  • 调整了一下布局

最终效果

Snipaste_2020-11-12_15-53-17.png

Snipaste_2020-11-12_15-53-38.png

BETA1.0

最终效果:

输入文件名和路径(可以设置默认值),点击Execute就可以在对应路径下创建一个模板文件。

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package sample;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main extends Application {

@Override
public void start(Stage primaryStage) {
VBox vBox = new VBox(20);
HBox hBox1 = new HBox(10);
HBox hBox2 = new HBox(10);
HBox hBox3 = new HBox(10);
vBox.getChildren().addAll(hBox1, hBox2, hBox3);

// HBox-1
Label nameLabel = new Label("File Name:");
TextField nameTextField = new TextField("LeetCode");
nameTextField.setMinWidth(250);

// HBox-2
Label pathLabel = new Label("Path:");
TextField pathTextField = new TextField("C:\\Users\\67460\\Documents\\blog\\source\\_posts");
pathTextField.setMinWidth(250);
Button browseButton = new Button("Browse");
browseButton.setOnAction(actionEvent -> pathTextField.setText(chooseDirectory(primaryStage).toString()));

// HBox-3
Label infoLabel = new Label();
// Set red color for info label
infoLabel.setTextFill(Paint.valueOf("#FF0000"));
Button executeButton = new Button("Execute");
executeButton.setOnAction(actionEvent -> {
try {
if (createTemplate(pathTextField.getText(), nameTextField.getText())) {
infoLabel.setText("Done!");
} else {
infoLabel.setText("The file has already existed!");
}
} catch (IOException e) {
e.printStackTrace();
}
});
Button cancelButton = new Button("Cancel");
cancelButton.setOnAction(actionEvent -> primaryStage.close());

hBox1.getChildren().addAll(nameLabel, nameTextField);
hBox2.getChildren().addAll(pathLabel, pathTextField, browseButton);
hBox3.getChildren().addAll(infoLabel, executeButton, cancelButton);

hBox1.setPadding(new Insets(20, 20, 10, 20));
hBox1.setAlignment(Pos.CENTER_LEFT);
hBox2.setPadding(new Insets(20));
hBox2.setAlignment(Pos.CENTER);
hBox3.setPadding(new Insets(20));
hBox3.setAlignment(Pos.CENTER_RIGHT);

primaryStage.setTitle("LeetCode template");
var image = new Image("LeetCodeIcon.png");
primaryStage.getIcons().add(image);
primaryStage.setScene(new Scene(vBox, 400, 240));
primaryStage.show();
}


public static void main(String[] args) {
launch(args);
}

/**
* @param name file name such as LeetCode-233
* @return return true if the file is created successfully and false if the file has already existed
* @throws IOException the operation to file may cause IOException
*/
private static boolean createTemplate(String path, String name) throws IOException {
File file = new File(path + "/" + name + ".md");
if (file.exists()) {
return false;
}

// build the content
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String content = String.format("""
---
title: %s
date: %s
tags:
categories: LeetCode
---

## 题目



<!-- more -->

## 结果



## 代码



## 复杂度

时间复杂度:O()

空间复杂度:O()
""", name, date);

// Write the content to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.flush();
return true;
}

private File chooseDirectory(Stage primaryStage) {
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Choose Directory");
return directoryChooser.showDialog(primaryStage);
}
}

With Go

年少不知CLI好,错把GUI当个宝。

以前总想写个图形界面出来,java的那个用gradle+javafx写的,启动速度好慢啊,费力不讨好,用go重写了一个。

参数

-title:post name

-tmpl:模板文件的路径,默认在./resources下

-post:hexo的post路径

Github

java:

https://github.com/carpediemtal/Blog-template

go:

https://github.com/carpediemtal/blog-template-with-go