diff options
Diffstat (limited to 'app/Issue/Text.hs')
-rw-r--r-- | app/Issue/Text.hs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/app/Issue/Text.hs b/app/Issue/Text.hs new file mode 100644 index 0000000..190e1c7 --- /dev/null +++ b/app/Issue/Text.hs @@ -0,0 +1,53 @@ +{-# LANGUAGE OverloadedRecordDot #-} +{-# LANGUAGE OverloadedStrings #-} + +module Issue.Text (extractText) where + +import Control.Arrow (second) +import Data.List (find) +import Data.Maybe (fromMaybe) +import Data.Text (Text) +import Data.Text qualified as T +import TreeGrepper.FileType (FileType) +import TreeGrepper.FileType qualified as G + +extractText :: FileType -> Text -> (Text, Maybe Text) +extractText fileType rawText = (title, description) + where + text = stripComments fileType $ stripLines rawText + stripLines = T.unlines . map T.strip . T.lines + (title, description') = + second T.stripStart $ + T.breakOn "\n\n" text + description + | T.null description' = Nothing + | otherwise = Just description' + +stripComments :: G.FileType -> Text -> Text +stripComments fileType text = + maybe + (stripLineComments (G.info fileType).lineStart text) + ( \(blockInfo, blockStart) -> + stripBlockComment blockStart blockInfo.blockEnd text + ) + $ do + blockInfo <- (G.info fileType).block + (,) blockInfo <$> find (`T.isPrefixOf` text) blockInfo.blockStart + +stripLineComments :: Text -> Text -> Text +stripLineComments lineStart text = + onLines + ( \line -> + fromMaybe line . fmap T.stripStart $ + T.stripPrefix lineStart line + ) + text + where + onLines f = T.unlines . map f . T.lines + +stripBlockComment :: Text -> Text -> Text -> Text +stripBlockComment blockStart blockEnd text = + T.strip + . (fromMaybe text . T.stripSuffix blockEnd) + . (fromMaybe text . T.stripPrefix blockStart) + $ text |