|
| 1 | +{-# LANGUAGE ScopedTypeVariables, FlexibleInstances, MultiParamTypeClasses, ConstraintKinds #-} |
| 2 | +module Distribution.Server.Features.PackageRank.Parser |
| 3 | + ( parseM |
| 4 | + ) where |
| 5 | + |
| 6 | + |
| 7 | +import Commonmark |
| 8 | +import Commonmark.Extensions |
| 9 | +import Control.Monad |
| 10 | +import Control.Monad.Identity |
| 11 | +import qualified Data.ByteString.Lazy as BS |
| 12 | + ( ByteString |
| 13 | + , toStrict |
| 14 | + ) |
| 15 | +import qualified Data.Text as T |
| 16 | +import qualified Data.Text.Encoding as T |
| 17 | +import qualified Data.Text.Encoding.Error as T |
| 18 | + ( lenientDecode ) |
| 19 | +import qualified Data.Text.IO as TIO |
| 20 | +import qualified Data.Text.Lazy.IO as TLIO |
| 21 | +import Data.Typeable ( Typeable ) |
| 22 | +import System.FilePath |
| 23 | + |
| 24 | +type MarkdownRenderable a b |
| 25 | + = (Typeable a, HasPipeTable a b, IsBlock a b, IsInline a) |
| 26 | + |
| 27 | +parseM :: BS.ByteString -> FilePath -> Either ParseError [MarkdownStats] |
| 28 | +parseM md name = runIdentity |
| 29 | + (commonmarkWith (pipeTableSpec <> defaultSyntaxSpec) name txt) |
| 30 | + where txt = T.decodeUtf8With T.lenientDecode . BS.toStrict $ md |
| 31 | + |
| 32 | +data MStats = MStats Int Int --number of pictures, number of chars |
| 33 | + deriving Show |
| 34 | + |
| 35 | +instance Monoid MStats where |
| 36 | + mempty = MStats 0 0 |
| 37 | + |
| 38 | +instance Rangeable MStats where |
| 39 | + ranged = const id |
| 40 | + |
| 41 | +instance HasAttributes MStats where |
| 42 | + addAttributes = const id |
| 43 | + |
| 44 | +instance Semigroup MStats where |
| 45 | + (MStats a b) <> (MStats c d) = MStats (a + c) (b + d) |
| 46 | + |
| 47 | +data MarkdownStats = NotImportant | |
| 48 | + HCode MStats | |
| 49 | + Code MStats | |
| 50 | + Section | -- Int? |
| 51 | + Table Int | |
| 52 | + PText MStats | |
| 53 | + List Int |
| 54 | + deriving (Show) |
| 55 | + |
| 56 | +sumMStat [] = mempty |
| 57 | +sumMStat (x : xs) = case x of |
| 58 | + NotImportant -> sumMStat xs |
| 59 | + Section -> sumMStat xs |
| 60 | + (List a) -> sumMStat xs |
| 61 | + (Table a) -> sumMStat xs |
| 62 | + (HCode a) -> a <> sumMStat xs |
| 63 | + (Code a) -> a <> sumMStat xs |
| 64 | + (PText a) -> a <> sumMStat xs |
| 65 | + |
| 66 | +instance Rangeable [MarkdownStats] where |
| 67 | + ranged = const id |
| 68 | + |
| 69 | +instance HasAttributes [MarkdownStats] where |
| 70 | + addAttributes = const id |
| 71 | + |
| 72 | +instance HasPipeTable MStats [MarkdownStats] where |
| 73 | + pipeTable _ _ rows = [Table $ length rows] |
| 74 | + |
| 75 | +instance IsInline MStats where |
| 76 | + lineBreak = MStats 0 1 |
| 77 | + softBreak = MStats 0 1 |
| 78 | + str t = MStats 0 (T.length t) |
| 79 | + entity t = MStats 0 (T.length t) |
| 80 | + escapedChar _ = MStats 0 1 |
| 81 | + emph = id |
| 82 | + strong = id |
| 83 | + link _ _ a = a |
| 84 | + image _ _ (MStats a b) = MStats (a + 1) b |
| 85 | + code t = MStats 0 (T.length t) |
| 86 | + rawInline _ t = MStats 0 (T.length t) |
| 87 | + |
| 88 | +instance IsBlock MStats [MarkdownStats] where |
| 89 | + paragraph a = [PText a] |
| 90 | + plain a = [PText a] |
| 91 | + thematicBreak = [NotImportant] |
| 92 | + blockQuote = id |
| 93 | + codeBlock language codeT | language == T.pack "haskell" = [HCode (code codeT)] |
| 94 | + | otherwise = [Code (code codeT)] |
| 95 | + heading _ _ = [Section] |
| 96 | + rawBlock _ r = [NotImportant] |
| 97 | + referenceLinkDefinition _ _ = [NotImportant] |
| 98 | + list _ _ l = [List (length l + depSum l)] |
| 99 | + |
| 100 | +depSum [] = 0 |
| 101 | +depSum ([] : xs) = depSum xs |
| 102 | +depSum ((List a : ys) : xs) = a + depSum (ys : xs) |
| 103 | +depSum ((_ : ys) : xs) = depSum (ys : xs) |
| 104 | + |
0 commit comments