Refactor properties code (#3104)

* enhance(properties): display various links well

* refactor(properties): split get-property-keys

* refactor(properties): add some testcases

Co-authored-by: leizhe <leizhe@leizhedeMacBook-Air.local>
pull/3111/head
llcc 2021-11-10 11:53:44 +08:00 committed by GitHub
parent 33b1e7831e
commit 793c814dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 20 deletions

View File

@ -7,7 +7,8 @@
[frontend.format.mldoc :as mldoc]
[frontend.handler.link :as link]
[frontend.text :as text]
[frontend.util.cursor :as cursor]))
[frontend.util.cursor :as cursor]
[frontend.handler.link :as link]))
(defonce properties-start ":PROPERTIES:")
(defonce properties-end ":END:")
@ -44,7 +45,7 @@
[content]
(if (contains-properties? content)
(string/replace content
(re-pattern ":PROPERTIES:\n:END:\n*")
(re-pattern ":PROPERTIES:\n+:END:\n*")
"")
content))
@ -77,28 +78,46 @@
(when-let [key (get-property-key line :org)]
(not (contains? #{:PROPERTIES :END} key))))))
(defn get-org-property-keys
[content]
(let [content-lines (string/split-lines content)
[_ properties&body] (split-with #(-> (string/triml %)
string/upper-case
(string/starts-with? properties-start)
not)
content-lines)
properties (rest (take-while #(-> (string/trim %)
string/upper-case
(string/starts-with? properties-end)
not
(or (string/blank? %)))
properties&body))]
(when (seq properties)
(map #(->> (string/split % ":")
(remove string/blank?)
first
string/upper-case)
properties))))
(defn get-markdown-property-keys
[content]
(let [content-lines (string/split-lines content)
properties (filter #(re-matches #"^.+::\s*.+" %) content-lines)]
(when (seq properties)
(map #(->> (string/split % "::")
(remove string/blank?)
first
string/upper-case)
properties))))
(defn get-property-keys
[format content]
(cond
(contains-properties? content)
(let [content-lines (string/split-lines content)
[_ properties&body] (split-with #(-> (string/triml %)
string/upper-case
(string/starts-with? properties-start)
not)
content-lines)
properties (rest (take-while #(-> (string/trim %)
string/upper-case
(string/starts-with? properties-end)
not
(or (string/blank? %)))
properties&body))]
(when (seq properties)
(map #(->> (string/split % ":")
(remove string/blank?)
first
string/upper-case)
properties)))))
(get-org-property-keys content)
(= :markdown format)
(get-markdown-property-keys content)))
(defn property-key-exist?
[format content key]

View File

@ -21,6 +21,21 @@
"hello\naa:: bb\nid:: f9873a81-07b9-4246-b910-53a6f5ec7e04\n\nworld"
"hello\naa:: bb\n\nworld")))
(deftest test-remove-empty-properties
(testing "remove properties if it is empty. Available in orgmode"
(are [x y] (= (property/remove-empty-properties x) y)
"* TODO properties demo\nabcd"
"* TODO properties demo\nabcd"
"* TODO properties demo\n:PROPERTIES:\n:END:\nabcd"
"* TODO properties demo\nabcd"
"* TODO properties demo\n:PROPERTIES:\n:END:\n\n\nabcd"
"* TODO properties demo\nabcd"
"* TODO properties demo\n:PROPERTIES:\n\n:END:\n\n\nabcd"
"* TODO properties demo\nabcd")))
(deftest test-remove-properties
(testing "properties with non-blank lines"
(are [x y] (= x y)
@ -55,6 +70,14 @@
(property/remove-properties :markdown "** hello\nx:: y\n\na:: b\n")
"** hello\n\na:: b")))
(deftest test-get-property-keys
(are [x y] (= x y)
(property/get-property-keys :org "hello\n:PROPERTIES:\n:x1: y1\n:x2: y2\n:END:\n")
["X1" "X2"]
(property/get-property-keys :org "hello\n:PROPERTIES:\n:END:\n")
nil))
(deftest test-insert-property
(are [x y] (= x y)
(property/insert-property :org "hello" "a" "b")
@ -74,6 +97,21 @@
#+END_QUOTE" "c" "d")
":PROPERTIES:\n:c: d\n:END:\n#+BEGIN_QUOTE\n hello world\n #+END_QUOTE"
(property/insert-property :org "hello
DEADLINE: <2021-10-25 Mon>
SCHEDULED: <2021-10-25 Mon>" "a" "b")
"hello\nSCHEDULED: <2021-10-25 Mon>\nDEADLINE: <2021-10-25 Mon>\n:PROPERTIES:\n:a: b\n:END:"
(property/insert-property :org "hello
DEADLINE: <2021-10-25 Mon>
SCHEDULED: <2021-10-25 Mon>\n:PROPERTIES:\n:a: b\n:END:\n" "c" "d")
"hello\nDEADLINE: <2021-10-25 Mon>\nSCHEDULED: <2021-10-25 Mon>\n:PROPERTIES:\n:a: b\n:c: d\n:END:"
(property/insert-property :org "hello
DEADLINE: <2021-10-25 Mon>
SCHEDULED: <2021-10-25 Mon>\n:PROPERTIES:\n:a: b\n:END:\nworld\n" "c" "d")
"hello\nDEADLINE: <2021-10-25 Mon>\nSCHEDULED: <2021-10-25 Mon>\n:PROPERTIES:\n:a: b\n:c: d\n:END:\nworld"
(property/insert-property :markdown "hello\na:: b\nworld\n" "c" "d")
"hello\na:: b\nc:: d\nworld"