Skip to content

Commit 3a21fba

Browse files
committed
Style minification
1 parent 77988f8 commit 3a21fba

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ Transforms code from usual html IntelliJ IDEA format into special _habr.com_ for
55

66
**Usage:**
77
* Single run mode
8-
* Select code region in IntelliJ IDEA
9-
* Press Ctrl-C (Copy)
8+
* Select code region in IntelliJ IDEA and press Ctrl-C (Copy)
109
* Run `java -jar code-converter.jar`
11-
* Press Ctrl-V (Paste)
10+
* Goto habr.com page editor and press Ctrl-V (Paste)
1211

1312
* Daemon mode
1413
* 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)
14+
* Select code region in IntelliJ IDEA and press Ctrl-C (Copy)
15+
* Goto habr.com page editor and press Ctrl-V (Paste)
16+
* Select code region in IntelliJ IDEA and press Ctrl-C (Copy)
17+
* Goto habr.com page editor and press Ctrl-V (Paste)~~~~
2118
...

src/main/kotlin/Main.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ val span = Regex("<span (.*?)>(.*?)</span>")
1313
val color = Regex("[\";]color:#(.*?)[\";]")
1414
val styleItalic = Regex("(font-style:italic)")
1515
val styleBold = Regex("(font-weight:bold)")
16+
val emptyFont = Regex("<font[^>]*?></font>")
17+
val uselessFont = Regex("<font[^>]*?>((&#32;)+)</font>")
1618

1719
val clipboard = Toolkit.getDefaultToolkit().systemClipboard!!
1820
val clipboardHtml
@@ -50,6 +52,7 @@ private fun convertClipboard() {
5052
private fun String.convertCode() =
5153
substring(pre)
5254
?.replace(span) { span -> span.groups[2]!!.value.styledWith(span.groups[1]!!.value) }
55+
?.popupBr()
5356
?.replaceTags()
5457
?.wrapWithCode()
5558

@@ -60,8 +63,19 @@ private fun String.putIntoClipboard() =
6063
private fun String.wrapWithCode() =
6164
"<code>$this</code>"
6265

66+
private fun String.popupBr() =
67+
// popup <br> from any style tags
68+
replace("<br></b>", "</b><br>")
69+
.replace("<br></i>", "</i><br>")
70+
.replace("<br></font>", "</font><br>")
71+
// useless style removing (minification)
72+
.replace("<b></b>", "")
73+
.replace("<i></i>", "")
74+
.replace(emptyFont, "")
75+
.replace(uselessFont, "$1")
76+
6377
private fun String.replaceTags() =
64-
replace("<br>", "\n")
78+
replace("<br>", "\n")
6579
.replace("&#32;", "&nbsp;")
6680

6781
private fun String.styledWith(style: String) =

0 commit comments

Comments
 (0)