Skip to content

Commit 77988f8

Browse files
committed
Daemon mode, stability, maven-shade
1 parent 49864bb commit 77988f8

3 files changed

Lines changed: 78 additions & 18 deletions

File tree

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@ Clipboard snippet code converter for http://habr.com.
44
Transforms code from usual html IntelliJ IDEA format into special _habr.com_ format.
55

66
**Usage:**
7-
* Select code region in IntelliJ IDEA
8-
* Press Ctrl-C (Copy)
9-
* Run `java -jar code-converter.jar`
10-
* Press Ctrl-V (Paste)
7+
* Single run mode
8+
* Select code region in IntelliJ IDEA
9+
* Press Ctrl-C (Copy)
10+
* Run `java -jar code-converter.jar`
11+
* Press Ctrl-V (Paste)
12+
13+
* Daemon mode
14+
* Run `java -jar code-converter.jar daemon`
15+
* Select code region in IntelliJ IDEA
16+
* Press Ctrl-C (Copy)
17+
* Press Ctrl-V (Paste)
18+
* Select code region in IntelliJ IDEA
19+
* Press Ctrl-C (Copy)
20+
* Press Ctrl-V (Paste)
21+
...

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@
9292
</archive>
9393
</configuration>
9494
</plugin>
95+
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-shade-plugin</artifactId>
99+
<version>3.2.1</version>
100+
<executions>
101+
<execution>
102+
<phase>package</phase>
103+
<goals>
104+
<goal>shade</goal>
105+
</goals>
106+
</execution>
107+
</executions>
108+
</plugin>
95109
</plugins>
96110
</build>
97111
</project>

src/main/kotlin/Main.kt

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.habr.codeconverter
33
import java.awt.Toolkit
44
import java.awt.datatransfer.DataFlavor
55
import java.awt.datatransfer.StringSelection
6+
import java.awt.datatransfer.UnsupportedFlavorException
67
import java.io.InputStream
78

89
val HTML_FLAVOR = DataFlavor("text/html", "Rich Formatted Text")
@@ -13,22 +14,56 @@ val color = Regex("[\";]color:#(.*?)[\";]")
1314
val styleItalic = Regex("(font-style:italic)")
1415
val styleBold = Regex("(font-weight:bold)")
1516

16-
fun main() {
17-
val cb = Toolkit.getDefaultToolkit().systemClipboard
18-
val source = (cb.getData(HTML_FLAVOR) as InputStream).reader().readText()
19-
println(source)
20-
StringSelection(
21-
"<code>" +
22-
source.substring(pre)!!
23-
.replace(span) {
24-
it.groups[2]!!.value.styledWith(it.groups[1]!!.value)
25-
}
26-
.replace("<br>", "\n")
27-
.replace("&#32;", "&nbsp;")
28-
+ "</code>"
29-
).let { cb.setContents(it, it) }
17+
val clipboard = Toolkit.getDefaultToolkit().systemClipboard!!
18+
val clipboardHtml
19+
get() =
20+
try {
21+
clipboard.getData(HTML_FLAVOR)
22+
} catch (e: UnsupportedFlavorException) {
23+
null
24+
}
25+
?.let { it as? InputStream }
26+
?.reader()
27+
?.readText()
28+
29+
fun main(vararg args: String) =
30+
if (args.isNotEmpty()) {
31+
println("Running as daemon")
32+
clipboard.addFlavorListener {
33+
convertClipboard()
34+
}.also {
35+
Thread.currentThread().suspend()
36+
}
37+
} else
38+
convertClipboard()
39+
40+
private fun convertClipboard() {
41+
clipboardHtml
42+
?.also {
43+
// debug only
44+
println("Source clipboard data as html:\n$it")
45+
}
46+
?.convertCode()
47+
?.putIntoClipboard()
3048
}
3149

50+
private fun String.convertCode() =
51+
substring(pre)
52+
?.replace(span) { span -> span.groups[2]!!.value.styledWith(span.groups[1]!!.value) }
53+
?.replaceTags()
54+
?.wrapWithCode()
55+
56+
private fun String.putIntoClipboard() =
57+
StringSelection(this)
58+
.let { clipboard.setContents(it, it) }
59+
60+
private fun String.wrapWithCode() =
61+
"<code>$this</code>"
62+
63+
private fun String.replaceTags() =
64+
replace("<br>", "\n")
65+
.replace("&#32;", "&nbsp;")
66+
3267
private fun String.styledWith(style: String) =
3368
styledWith(
3469
style.substring(color),

0 commit comments

Comments
 (0)