Mercurial > cgi-bin > hgweb.cgi > ClipMan
annotate src/name/blackcap/clipman/Troff.kt @ 57:c6cccbe2f393
Port to OpenJDK 15 (mostly done).
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 23 Mar 2022 23:56:29 -0700 |
parents | 22725d4d7849 |
children |
rev | line source |
---|---|
56
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
1 /* |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
2 * Coercion to troff input. |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
3 */ |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
4 package name.blackcap.clipman |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
5 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
6 import org.jsoup.Jsoup |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
7 import org.jsoup.nodes.* |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 import org.jsoup.select.NodeVisitor |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 class Troffizer: NodeVisitor { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
11 private enum class Typeface(val pos: Int) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
12 ROMAN(1), |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
13 ITALIC(2), |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
14 BOLD(3) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
15 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
16 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 private val tfStack = mutableListOf<Typeface>(Typeface.ROMAN); |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 private val TF_TAGS = mapOf<String, Typeface>( |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 "em" to Typeface.ITALIC, |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 "i" to Typeface.ITALIC, |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 "b" to Typeface.BOLD, |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 "strong" to Typeface.BOLD) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
25 private val accum = StringBuilder(); |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 override fun head(node: Node, depth: Int): Unit { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
28 when (node) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
29 is TextNode -> accum.append(node.text()) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 is Element -> enterElement(node) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
31 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
33 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
34 override fun tail(node: Node, depth: Int): Unit { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
35 if (node is Element) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
36 leaveElement(node) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
37 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
39 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
40 private fun enterElement(element: Element) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 var newFace = TF_TAGS[element.normalName()] |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
42 if (newFace != null) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
43 tfStack.add(newFace) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
44 accum.append("\\f") |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
45 accum.append(newFace.pos) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
47 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 private fun leaveElement(element: Element) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 if (element.normalName() in TF_TAGS) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 tfStack.removeLast() |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 accum.append("\\f") |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 accum.append(tfStack.lastOrNull()?.pos ?: Typeface.ROMAN.pos) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 fun getTroff(): String = accum.toString() |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
58 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
59 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
60 private fun _troffize(html: String): String { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 val troffizer = Troffizer() |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 Jsoup.parse(html).traverse(troffizer) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
63 return troffizer.getTroff() |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
64 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
65 |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 fun troffize(item: PasteboardItem): Unit { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
67 val (plain, html) = when (item) { |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
68 is PasteboardItem.Plain -> |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
69 Pair(item.plain, null) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
70 is PasteboardItem.HTML -> |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
71 Pair(item.plain, item.html) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
72 is PasteboardItem.RTF -> |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
73 Pair(item.plain, item.html) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
74 } |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
75 PasteboardItem.write( |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
76 PasteboardItem.Plain( |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
77 if (html == null) { plain } else { _troffize(html) })) |
22725d4d7849
An attempt to get it to troff-ize styled text.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
78 } |