mirror of
https://github.com/journeyman-cc/smeagol.git
synced 2026-04-12 18:05:06 +00:00
refactor parse out of include ns
This commit is contained in:
parent
4a4269d202
commit
6714dc04bf
4 changed files with 133 additions and 90 deletions
|
|
@ -2,7 +2,8 @@
|
|||
(:require
|
||||
[schema.core :as s]
|
||||
[com.stuartsierra.component :as component]
|
||||
[smeagol.include.resolver :as resolver]))
|
||||
[smeagol.include.parse :as parse]
|
||||
[smeagol.include.resolve :as resolve]))
|
||||
|
||||
(s/defrecord Includer
|
||||
[resolver])
|
||||
|
|
@ -15,52 +16,12 @@
|
|||
(extend-type Includer
|
||||
IncludeMd
|
||||
(expand-include-md [includer md-src]
|
||||
;parse md-src
|
||||
(let [includes (parse/parse-include-md md-src)]
|
||||
;resolve found includes
|
||||
;indent & integrate
|
||||
md-src))
|
||||
md-src)))
|
||||
|
||||
(s/defn
|
||||
new-includer
|
||||
[]
|
||||
(map->Includer {}))
|
||||
|
||||
(def IncludeLink
|
||||
{:uri s/Str
|
||||
:indent-heading s/Num
|
||||
:indent-list s/Num})
|
||||
|
||||
(s/defn
|
||||
convert-indent-to-int :- s/Num
|
||||
[indents :- [s/Str]]
|
||||
(if (some? indents)
|
||||
(Integer/valueOf (nth indents 2))
|
||||
0))
|
||||
|
||||
(s/defn
|
||||
parse-indent-list
|
||||
[md-src :- s/Str]
|
||||
(re-matches #".*(:indent-list (\d)).*" md-src))
|
||||
|
||||
(s/defn
|
||||
parse-indent-heading
|
||||
[md-src :- s/Str]
|
||||
(re-matches #".*(:indent-heading (\d)).*" md-src))
|
||||
|
||||
(s/defn
|
||||
parse-include-link
|
||||
[md-src :- s/Str]
|
||||
(re-seq #".*&\[\w*(.*)\w*\]\((.*)\).*" md-src))
|
||||
|
||||
(s/defn
|
||||
parse-include-md :- [IncludeLink]
|
||||
[md-src :- s/Str]
|
||||
(vec
|
||||
(map
|
||||
(fn [parse-element]
|
||||
(let [uri (nth parse-element 2)
|
||||
indents-text (nth parse-element 1)]
|
||||
{:uri uri
|
||||
:indent-heading (convert-indent-to-int (parse-indent-heading indents-text))
|
||||
:indent-list (convert-indent-to-int (parse-indent-list indents-text))}))
|
||||
(parse-include-link md-src))))
|
||||
|
|
|
|||
43
src/smeagol/include/parse.clj
Normal file
43
src/smeagol/include/parse.clj
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
(ns smeagol.include.parse
|
||||
(:require
|
||||
[schema.core :as s]))
|
||||
|
||||
(def IncludeLink
|
||||
{:uri s/Str
|
||||
:indent-heading s/Num
|
||||
:indent-list s/Num})
|
||||
|
||||
(s/defn
|
||||
convert-indent-to-int :- s/Num
|
||||
[indents :- [s/Str]]
|
||||
(if (some? indents)
|
||||
(Integer/valueOf (nth indents 2))
|
||||
0))
|
||||
|
||||
(s/defn
|
||||
parse-indent-list
|
||||
[md-src :- s/Str]
|
||||
(re-matches #".*(:indent-list (\d)).*" md-src))
|
||||
|
||||
(s/defn
|
||||
parse-indent-heading
|
||||
[md-src :- s/Str]
|
||||
(re-matches #".*(:indent-heading (\d)).*" md-src))
|
||||
|
||||
(s/defn
|
||||
parse-include-link
|
||||
[md-src :- s/Str]
|
||||
(re-seq #".*&\[\w*(.*)\w*\]\((.*)\).*" md-src))
|
||||
|
||||
(s/defn
|
||||
parse-include-md :- [IncludeLink]
|
||||
[md-src :- s/Str]
|
||||
(vec
|
||||
(map
|
||||
(fn [parse-element]
|
||||
(let [uri (nth parse-element 2)
|
||||
indents-text (nth parse-element 1)]
|
||||
{:uri uri
|
||||
:indent-heading (convert-indent-to-int (parse-indent-heading indents-text))
|
||||
:indent-list (convert-indent-to-int (parse-indent-list indents-text))}))
|
||||
(parse-include-link md-src))))
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
(:require [clojure.test :refer :all]
|
||||
[schema.core :as s]
|
||||
[com.stuartsierra.component :as component]
|
||||
[smeagol.include.resolver :as resolver]
|
||||
[smeagol.include.resolve :as resolve]
|
||||
[smeagol.include :as sut]))
|
||||
|
||||
(def include-simple
|
||||
|
|
@ -40,49 +40,7 @@ some text
|
|||
&[](./simple.md)
|
||||
more text.")
|
||||
|
||||
|
||||
(deftest test-parse-include-md
|
||||
(testing "parse include links"
|
||||
(is
|
||||
(= []
|
||||
(sut/parse-include-md "# Heading")))
|
||||
(is
|
||||
(= [{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-simple)))
|
||||
(is
|
||||
(= [{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-surounding-simple)))
|
||||
(is
|
||||
(= [{:uri "./with-heading.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-heading-0)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md", :indent-heading 1, :indent-list 1}]
|
||||
(sut/parse-include-md
|
||||
include-heading-list-1)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-heading-list-0)))
|
||||
(is
|
||||
(= [{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-invalid-indent)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md", :indent-heading 2, :indent-list 3}]
|
||||
(sut/parse-include-md
|
||||
include-spaced-indent)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md",
|
||||
:indent-heading 2,
|
||||
:indent-list 3}
|
||||
{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
multi)))))
|
||||
|
||||
(s/defmethod resolver/do-resolve-md :test-mock
|
||||
(s/defmethod resolve/do-resolve-md :test-mock
|
||||
[resolver
|
||||
uri :- s/Str]
|
||||
(cond
|
||||
|
|
@ -90,7 +48,7 @@ more text.")
|
|||
|
||||
(def system-under-test
|
||||
(component/system-map
|
||||
:resolver (resolver/new-resolver :test-mock)
|
||||
:resolver (resolve/new-resolver :test-mock)
|
||||
:includer (component/using
|
||||
(sut/new-includer)
|
||||
{:resolver :resolver})))
|
||||
|
|
|
|||
81
test/smeagol/test/include/parse.clj
Normal file
81
test/smeagol/test/include/parse.clj
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
(ns smeagol.test.include.parse
|
||||
(:require [clojure.test :refer :all]
|
||||
[schema.core :as s]
|
||||
[smeagol.include.parse :as sut]))
|
||||
|
||||
(def include-simple
|
||||
"# Heading1
|
||||
&[](./simple.md)")
|
||||
|
||||
(def include-surounding-simple
|
||||
"# Heading1
|
||||
Some surounding &[](./simple.md) text")
|
||||
|
||||
(def include-heading-0
|
||||
"# Heading1
|
||||
&[:indent-heading 0](./with-heading.md)")
|
||||
|
||||
(def include-heading-list-1
|
||||
"# Heading1
|
||||
&[:indent-heading 1 :indent-list 1](./with-heading-and-list.md)")
|
||||
|
||||
(def include-heading-list-0
|
||||
"# Heading1
|
||||
&[:indent-list 0 :indent-heading 0](./with-heading-and-list.md)")
|
||||
|
||||
(def include-invalid-indent
|
||||
"# Heading1
|
||||
&[ invalid input should default to indent 0 ](./simple.md)")
|
||||
|
||||
(def include-spaced-indent
|
||||
"# Heading1
|
||||
&[ :indent-heading 2 :indent-list 33 ](./with-heading-and-list.md)")
|
||||
|
||||
(def multi
|
||||
"# Heading1
|
||||
&[ :indent-heading 2 :indent-list 33 ](./with-heading-and-list.md)
|
||||
some text
|
||||
&[](./simple.md)
|
||||
more text.")
|
||||
|
||||
|
||||
(deftest test-parse-include-md
|
||||
(testing "parse include links"
|
||||
(is
|
||||
(= []
|
||||
(sut/parse-include-md "# Heading")))
|
||||
(is
|
||||
(= [{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-simple)))
|
||||
(is
|
||||
(= [{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-surounding-simple)))
|
||||
(is
|
||||
(= [{:uri "./with-heading.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-heading-0)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md", :indent-heading 1, :indent-list 1}]
|
||||
(sut/parse-include-md
|
||||
include-heading-list-1)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-heading-list-0)))
|
||||
(is
|
||||
(= [{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
include-invalid-indent)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md", :indent-heading 2, :indent-list 3}]
|
||||
(sut/parse-include-md
|
||||
include-spaced-indent)))
|
||||
(is
|
||||
(= [{:uri "./with-heading-and-list.md",
|
||||
:indent-heading 2,
|
||||
:indent-list 3}
|
||||
{:uri "./simple.md", :indent-heading 0, :indent-list 0}]
|
||||
(sut/parse-include-md
|
||||
multi)))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue