1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Track contents as children of the heading

This commit is contained in:
Timothy Clem 2017-11-20 14:20:23 -08:00
parent 631594e01e
commit 4aedbf9647
3 changed files with 7 additions and 15 deletions

View File

@ -8,13 +8,13 @@ module Language.Markdown.Assignment
import qualified CMarkGFM
import Data.ByteString (ByteString)
import Data.Function (on)
import Data.Functor (void)
import Data.Record
import Data.Syntax (makeTerm)
import qualified Data.Syntax as Syntax
import Data.Syntax.Assignment hiding (Assignment, Error)
import qualified Data.Syntax.Assignment as Assignment
import Data.Term as Term (Term(..), TermF(..), termIn, unwrap)
import Data.Term as Term (Term(..), TermF(..), termIn)
import qualified Data.Text as Text
import Data.Text.Encoding (encodeUtf8)
import Data.Union
@ -30,7 +30,6 @@ type Syntax =
, Markup.HTMLBlock
, Markup.OrderedList
, Markup.Paragraph
, Markup.Section
, Markup.ThematicBreak
, Markup.UnorderedList
, Markup.Table
@ -68,7 +67,7 @@ blockElement = choice
, codeBlock
, thematicBreak
, htmlBlock
, section
, heading
, table
]
@ -83,8 +82,8 @@ list = termIn <$> symbol List <*> ((\ (CMarkGFM.LIST CMarkGFM.ListAttributes{..}
item :: Assignment
item = makeTerm <$> symbol Item <*> children (many blockElement)
section :: Assignment
section = makeTerm <$> symbol Heading <*> ((\ (CMarkGFM.HEADING level) -> Markup.Heading level) . termAnnotation . termOut <$> currentNode <*> children (many inlineElement))
heading :: Assignment
heading = makeTerm <$> symbol Heading <*> ((\ (CMarkGFM.HEADING level) -> Markup.Heading level) . termAnnotation . termOut <$> currentNode <*> children (many inlineElement) <*> manyTill blockElement (void (symbol Heading) <|> eof))
blockQuote :: Assignment
blockQuote = makeTerm <$> symbol BlockQuote <*> children (Markup.BlockQuote <$> many blockElement)

View File

@ -28,14 +28,7 @@ instance Eq1 Paragraph where liftEq = genericLiftEq
instance Ord1 Paragraph where liftCompare = genericLiftCompare
instance Show1 Paragraph where liftShowsPrec = genericLiftShowsPrec
data Section a = Section { sectionLevel :: Int, sectionHeading :: a, sectionContent :: [a] }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable)
instance Eq1 Section where liftEq = genericLiftEq
instance Ord1 Section where liftCompare = genericLiftCompare
instance Show1 Section where liftShowsPrec = genericLiftShowsPrec
data Heading a = Heading { headingLevel :: Int, headingContent :: [a] }
data Heading a = Heading { headingLevel :: Int, headingContent :: [a], sectionContent :: [a] }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable)
instance Eq1 Heading where liftEq = genericLiftEq

View File

@ -133,7 +133,7 @@ class CustomHasDeclaration syntax where
-- | Produce a 'HeadingDeclaration' from the first line of the heading of a 'Markdown.Heading' node.
instance CustomHasDeclaration Markdown.Heading where
customToDeclaration Blob{..} ann (Markdown.Heading level terms)
customToDeclaration Blob{..} ann (Markdown.Heading level terms _)
= Just $ HeadingDeclaration (headingText terms) level
where headingText terms = getSource $ maybe (byteRange ann) sconcat (nonEmpty (fmap (\ (Term (In ann _), _) -> (byteRange ann)) (toList terms)))
getSource = firstLine . toText . flip Source.slice blobSource