-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline.clj
124 lines (105 loc) · 3.64 KB
/
inline.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(ns cljmd.re.inline
(:require [clojure.string :as string]
[flatland.useful.fn :as ufn]
[cljmd.util :as util]
[cljmd.re.common :refer :all]
[cljmd.re.link :as link]))
(def code-span
(let [pre "(?<!`)"
post "(?!`)"]
(re-pattern (str "(?s)" pre (unescaped "(`+)") post
"(.*?)"
pre #"\1" post))))
(defn delimiter-run
[char-class]
(re-pattern (str "(?<!" (unescaped char-class) ")"
(unescaped char-class) "+"
"(?!" char-class ")")))
(defn lfdr-nopunc
[delimiter]
(re-pattern (str delimiter
#"(?!\p{IsWhite_Space}|$)"
#"(?!\p{IsPunctuation})")))
(defn lfdr-punc
[delimiter]
(re-pattern (str #"(?<=^|\p{IsWhite_Space}|\p{IsPunctuation})"
delimiter
#"(?!\p{IsWhite_Space}|$)"
#"(?=\p{IsPunctuation})")))
(def lfdr
(comp (ufn/ap util/or-re) (juxt lfdr-nopunc lfdr-punc)))
(defn rfdr-punc
[delimiter]
(re-pattern (str #"(?<=\p{IsPunctuation})"
#"(?<!^|\p{IsWhite_Space})"
delimiter
#"(?=\p{IsWhite_Space}|\p{IsPunctuation}|$)")))
(defn rfdr-nopunc
[delimiter]
(re-pattern (str #"(?<!\p{IsPunctuation})"
#"(?<!^|\p{IsWhite_Space})"
delimiter)))
(def rfdr
(comp (ufn/ap util/or-re) (juxt rfdr-nopunc rfdr-punc)))
(defn lobar-open-emphasis
[delimiter]
(let [left (lfdr delimiter)
right (rfdr delimiter)
punctuated-right (rfdr-punc delimiter)]
(re-pattern (str (util/or-re (str "(?=" left ")" "(?!" right ")")
(str "(?=" left ")" "(?=" punctuated-right ")"))
left))))
(defn lobar-close-emphasis
[delimiter]
(let [left (lfdr delimiter)
punctuated-left (lfdr-punc delimiter)
right (rfdr delimiter)]
(re-pattern (str (util/or-re (str "(?=" right ")" "(?!" left ")")
(str "(?=" right ")" "(?=" punctuated-left ")"))
right))))
(def autolink
(re-pattern (str (unescaped \<)
(util/or-re (str "(" absolute-uri ")")
(str "(" email-address ")"))
">")))
(def hard-line-break
(let [end #"(?:\r\n|\n|\r(?!\n)|$)"]
(util/or-re (str #"(?<=\p{Print}) " end)
(str (unescaped #"\\") end))))
(def soft-line-break
(re-pattern (str #"(?<=.)(?<!(?:[ ]{2,}|\\))"
line-ending
#"(?=.)")))
(def image-description
(re-pattern (str (unescaped \!) balanced-square-brackets)))
(def inline-image
(re-pattern (str image-description
#"\("
#"\s*"
link/destination "?"
"(?:" #"\s+" "(" link/title ")" ")?"
#"\s*"
#"\)")))
(defn full-image-reference
[labels]
(if (empty? labels)
unmatchable
(re-pattern (str #"(?u)(?i)" image-description
(apply util/or-re (map link/label-matcher labels))))))
(defn collapsed-image-reference
[labels]
(if (empty? labels)
unmatchable
(re-pattern (str #"(?u)(?i)"
(unescaped \!)
(apply util/or-re (map link/label-matcher labels))
#"\[\]"))))
(defn shortcut-image-reference
[labels]
(if (empty? labels)
unmatchable
(re-pattern (str #"(?u)(?i)"
(unescaped \!)
(apply util/or-re (map link/label-matcher labels))
#"(?!\[\])"
"(?!" link/label ")"))))