diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-11-29 05:39:32 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-11-29 05:44:37 +0100 |
commit | c2e4b0a28c050c52f6ed84d6ae1e7172e4b6b08a (patch) | |
tree | 7ea64f2a1bb4f3d78786d81d4603e1479c3087dd /app/Issue/Text.hs | |
parent | 8c0d5f70b4a2e5928dce73769d46a79b2b051f63 (diff) |
support comments
Diffstat (limited to 'app/Issue/Text.hs')
-rw-r--r-- | app/Issue/Text.hs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/app/Issue/Text.hs b/app/Issue/Text.hs index 4cfc5f7..cb4ae47 100644 --- a/app/Issue/Text.hs +++ b/app/Issue/Text.hs @@ -2,10 +2,12 @@ module Issue.Text ( extractText, stripIssueMarkers, issueMarkers, + extractComments, ) where -import Control.Arrow (first, second) +import CMark qualified as D +import Control.Arrow (first, second, (***)) import Data.Text (Text) import Data.Text qualified as T @@ -32,3 +34,29 @@ stripIssueMarkers text = T.stripStart (T.drop (T.length marker) text) [] -> ([], text) + +extractComments :: T.Text -> ([T.Text], T.Text) +extractComments text = collect (D.commonmarkToNode [] text) + where + collect :: D.Node -> ([T.Text], T.Text) + collect (D.Node _ D.DOCUMENT ns) = + (map (toText . reverse) . reverse) + *** (toText . reverse) + $ collect' ([], []) ns + collect _ = error "commonmarkToNode: no document" + + collect' :: ([[D.Node]], [D.Node]) -> [D.Node] -> ([[D.Node]], [D.Node]) + collect' (as, rs) [] = (as, rs) + collect' ([], rs) (n@(D.Node _ D.PARAGRAPH (n' : _)) : ns) + | D.Node _ (D.TEXT s) _ <- n', + T.isPrefixOf "COMMENT" s = + collect' ([[n]], rs) ns + | otherwise = collect' ([], n : rs) ns + collect' (ass@(a : as), rs) (n@(D.Node _ D.PARAGRAPH (n' : _)) : ns) + | D.Node _ (D.TEXT s) _ <- n', + T.isPrefixOf "COMMENT" s = + collect' (([n] : ass), rs) ns + | otherwise = collect' (((n : a) : as), rs) ns + collect' (as, rs) (_ : ns) = collect' (as, rs) ns + + toText = D.nodeToCommonmark [] Nothing . D.Node Nothing D.DOCUMENT |