-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.clj
72 lines (59 loc) · 2.35 KB
/
run.clj
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
(ns docx-utils.elements.run
(:require [clojure.tools.logging :as log])
(:import (org.apache.poi.xwpf.usermodel XWPFRun XWPFParagraph TextSegement)
(org.openxmlformats.schemas.wordprocessingml.x2006.main STHighlightColor$Enum)))
(defn- configure-run [^XWPFRun run {:keys [text pos bold highlight-color]
:or {text ""
pos 0
highlight-color "none"}}]
(.setText run (if (string? text) text (str text)) pos)
(.setBold run (or bold false))
(-> run (.getCTR) (.addNewRPr) (.addNewHighlight) (.setVal (STHighlightColor$Enum/forString highlight-color))))
(defn- boolean? [value]
(when (or (true? value) (false? value))
true))
(defmulti set-run (fn [run value] (cond
(string? value) :string
(number? value) :number
(map? value) :map
(boolean? value) :boolean
:else :not-supported)))
(defmethod set-run :string [run value]
(configure-run run {:text value}))
(defmethod set-run :number [run value]
(configure-run run {:text (str value)}))
(defmethod set-run :boolean [run value]
(configure-run run {:text (str value)}))
(defmethod set-run :map [run value]
(configure-run run value))
(defmethod set-run :default [run value]
(log/warnf "Not supported value: %s" value))
(defn run-ids-to-text
[^XWPFParagraph par run-range]
(let [runs (.getRuns par)]
(reduce
(fn [string run-id]
(str string (.getText (.get runs run-id) 0)))
""
run-range)))
(defn remove-rest-runs!
[^XWPFParagraph par run-range]
(reduce
(fn [_ run-id]
(.removeRun par run-id))
nil
(reverse (rest run-range))))
(defn find-first-found-run [^XWPFParagraph par ^TextSegement found-segment]
(.get (.getRuns par)
(.getBeginRun found-segment)))
(defn run-id-range [^TextSegement found-segment]
(range
(.getBeginRun found-segment)
(inc (.getEndRun found-segment))))
(defn merge-runs!
[^XWPFParagraph par ^TextSegement found-segment]
(let [run-ids (run-id-range found-segment)]
(.setText
(find-first-found-run par found-segment)
(run-ids-to-text par run-ids) 0)
(remove-rest-runs! par (run-id-range found-segment))))