-
Notifications
You must be signed in to change notification settings - Fork 2
/
create-blogpost.kts
executable file
·169 lines (132 loc) · 5.13 KB
/
create-blogpost.kts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env kscript
@file:DependsOn("info.picocli:picocli:4.2.0")
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters
import java.io.File
import java.nio.file.*
import java.util.concurrent.Callable
import java.time.*
import java.time.format.*
import java.util.concurrent.*
@Command(name = "create-blogpost", mixinStandardHelpOptions = true, version = ["1.0"])
class GenerateSnippets : Callable<Int> {
@Option(names = ["-t", "--title"], description = ["Title of the blogpost"])
private var title: String = ""
@Option(names = ["-i", "--image"], paramLabel = "IMAGE", description = ["The input .yml file to read from"])
private var image: String = ""
override fun call(): Int {
info("🖋🖋🖋 create-blogpost ✒️✒️✒️️️️", "")
info("Welcome to create-blogpost", "👋")
info("Creating your blogpost...", "")
val date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
val id = title.toLowerCase().replace(" ", "-")
val filename = "./_posts/$date-$id.md"
val imageFolder = "./assets/images/posts/$id"
val headerFilename = "$imageFolder/header.jpg"
val teaserFilename = "$imageFolder/teaser.jpg"
// language=YAML
val content = """
---
title: "$title"
categories: "Android"
published: false
excerpt: "TODO"
header:
image: "$headerFilename"
teaser: "$teaserFilename"
caption: "Royal Djurgården - Stockholm, Sweden"
---
# Source Code
{% highlight kotlin linenos %}
val thisIs = "Some kotlin code with line numbers"
{% endhighlight %}
# Notice Boxes
Single line Info Box
{: .notice--info}
Single line Warning Box
{: .notice--warning}
Single line Danger Box
{: .notice--danger}
{% capture notice-info %}
**Multi Line Info Box**
Where you can put code, images and whatsoever needed
{% endcapture %}
<div class="notice--info">{{ notice-info | markdownify }}</div>
# Images
![sample-image](${headerFilename.drop(1)})
<figure>
<img src="${headerFilename.drop(1)}" alt="image with caption">
<figcaption>Caption goes here</figcaption>
</figure>
""".trimIndent()
File(filename).writeText(content)
if (image.isNotBlank()) {
info("Resizing your image...", "")
Files.createDirectories(Paths.get(imageFolder));
"mogrify -resize 1920x -quality 85 -write $headerFilename $image".runCommand()
"mogrify -resize 600x -quality 85 -write $teaserFilename $image".runCommand()
} else {
warn("No image provided, skipping resizing.")
}
info("Blogpost title: $title", "")
info("Blogpost id: $id", "")
info("Blogpost file: $filename", "")
info("Header file: $headerFilename", "")
info("Teaser file: $teaserFilename", "")
succ("Blogpost created successfully!")
return 0
}
/*
* DEBUG Prints function
******************************************************************/
fun error(message: String, throwable: Throwable? = null, statusCode: Int = 1): Nothing {
System.err.println("❌\t${Colors.ANSI_RED}$message${Colors.ANSI_RESET}")
throwable?.let {
System.err.print(Colors.ANSI_RED)
it.printStackTrace()
System.err.print(Colors.ANSI_RESET)
}
System.exit(statusCode)
throw Error()
}
fun warn(message: String) {
System.out.println("⚠️\t${Colors.ANSI_YELLOW}$message${Colors.ANSI_RESET}")
}
fun succ(message: String) {
System.out.println("✅\t${Colors.ANSI_GREEN}$message${Colors.ANSI_RESET}")
}
fun info(message: String, emoji: String = "ℹ️") {
System.out.println("$emoji\t$message")
}
fun String.runCommand(
workingDir: File = File("."),
timeoutAmount: Long = 60
): String? = try {
ProcessBuilder(split("\\s".toRegex()))
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start().apply { waitFor(timeoutAmount, TimeUnit.SECONDS) }
.inputStream.bufferedReader().readText()
} catch (e: java.io.IOException) {
e.printStackTrace()
null
}
}
CommandLine(GenerateSnippets()).execute(*args)
/*
* ASCII Color
******************************************************************/
object Colors {
val ANSI_RESET = "\u001B[0m"
val ANSI_BLACK = "\u001B[30m"
val ANSI_RED = "\u001B[31m"
val ANSI_GREEN = "\u001B[32m"
val ANSI_YELLOW = "\u001B[33m"
val ANSI_BLUE = "\u001B[34m"
val ANSI_PURPLE = "\u001B[35m"
val ANSI_CYAN = "\u001B[36m"
val ANSI_WHITE = "\u001B[37m"
}