Skip to content

Commit 4d69545

Browse files
authored
Merge pull request #35 from Rosuavio/tasklist
Add TASKLIST node type
2 parents 7b8fbbc + 0af421f commit 4d69545

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

CMarkGFM.hsc

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Data.Maybe (fromMaybe)
4646
import GHC.Generics (Generic)
4747
import Data.Data (Data)
4848
import Data.Typeable (Typeable)
49-
import Data.Text (Text, empty, snoc)
49+
import Data.Text (Text, empty, pack, snoc)
5050
import qualified Data.Text.Foreign as TF
5151
import Data.ByteString.Unsafe (unsafePackMallocCString)
5252
import Data.Text.Encoding (decodeUtf8)
@@ -239,6 +239,7 @@ data NodeType =
239239
| HEADING Level
240240
| LIST ListAttributes
241241
| ITEM
242+
| TASKLIST Bool
242243
| TEXT Text
243244
| SOFTBREAK
244245
| LINEBREAK
@@ -335,7 +336,11 @@ ptrToNodeType ptr = do
335336
#const CMARK_NODE_LIST
336337
-> LIST <$> listAttr
337338
#const CMARK_NODE_ITEM
338-
-> return ITEM
339+
-> do
340+
ts <- typeString
341+
if ts == pack "tasklist"
342+
then TASKLIST <$> checked
343+
else return ITEM
339344
#const CMARK_NODE_HEADING
340345
-> HEADING <$> level
341346
#const CMARK_NODE_EMPH
@@ -372,7 +377,8 @@ ptrToNodeType ptr = do
372377
return TABLE_CELL
373378
else
374379
error $ "Unknown node type " ++ (show nodeType)
375-
where literal = c_cmark_node_get_literal ptr >>= totext
380+
where typeString = c_cmark_node_get_type_string ptr >>= totext
381+
literal = c_cmark_node_get_literal ptr >>= totext
376382
level = c_cmark_node_get_heading_level ptr
377383
onEnter = c_cmark_node_get_on_enter ptr >>= totext
378384
onExit = c_cmark_node_get_on_exit ptr >>= totext
@@ -400,6 +406,7 @@ ptrToNodeType ptr = do
400406
ncols <- c_cmark_gfm_extensions_get_table_columns ptr
401407
cols <- c_cmark_gfm_extensions_get_table_alignments ptr
402408
mapM (fmap ucharToAlignment . peekElemOff cols) [0..(fromIntegral ncols) - 1]
409+
checked = c_cmark_gfm_extensions_get_tasklist_item_checked ptr
403410
ucharToAlignment (CUChar 108) = LeftAligned
404411
ucharToAlignment (CUChar 99) = CenterAligned
405412
ucharToAlignment (CUChar 114) = RightAligned
@@ -464,7 +471,13 @@ fromNode (Node _ nodeType children) = do
464471
c_cmark_node_set_list_tight n $ listTight attr
465472
c_cmark_node_set_list_start n $ listStart attr
466473
return n
467-
ITEM -> c_cmark_node_new (#const CMARK_NODE_ITEM)
474+
ITEM -> c_cmark_node_new (#const CMARK_NODE_ITEM)
475+
TASKLIST checked -> do
476+
n <- c_cmark_node_new (#const CMARK_NODE_ITEM)
477+
tasklistExt <- withCString (unCMarkExtension extTaskList) c_cmark_find_syntax_extension
478+
c_cmark_node_set_syntax_extension n tasklistExt
479+
c_cmark_gfm_extensions_set_tasklist_item_checked n checked
480+
return n
468481
HEADING lev -> do
469482
n <- c_cmark_node_new (#const CMARK_NODE_HEADING)
470483
c_cmark_node_set_heading_level n lev
@@ -555,6 +568,9 @@ foreign import ccall "cmark-gfm.h cmark_parser_free"
555568
foreign import ccall "cmark-gfm.h cmark_node_get_type"
556569
c_cmark_node_get_type :: NodePtr -> IO Int
557570

571+
foreign import ccall "cmark-gfm.h cmark_node_get_type_string"
572+
c_cmark_node_get_type_string :: NodePtr -> IO CString
573+
558574
foreign import ccall "cmark-gfm.h cmark_node_first_child"
559575
c_cmark_node_first_child :: NodePtr -> IO NodePtr
560576

@@ -680,3 +696,12 @@ foreign import ccall "cmark-gfm-core-extensions.h cmark_gfm_extensions_get_table
680696

681697
foreign import ccall "cmark-gfm-core-extensions.h cmark_gfm_extensions_get_table_alignments"
682698
c_cmark_gfm_extensions_get_table_alignments :: NodePtr -> IO (Ptr CUChar)
699+
700+
foreign import ccall "cmark-gfm-core-extensions.h cmark_node_set_syntax_extension"
701+
c_cmark_node_set_syntax_extension :: NodePtr -> ExtensionPtr -> IO Int
702+
703+
foreign import ccall "cmark-gfm-core-extensions.h cmark_gfm_extensions_set_tasklist_item_checked"
704+
c_cmark_gfm_extensions_set_tasklist_item_checked :: NodePtr -> Bool -> IO Int
705+
706+
foreign import ccall "cmark-gfm-core-extensions.h cmark_gfm_extensions_get_tasklist_item_checked"
707+
c_cmark_gfm_extensions_get_tasklist_item_checked :: NodePtr -> IO Bool

test/test-cmark.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ tests = TestList [
3434
, "&lt;xmp>\n" ~=? commonmarkToHtml [optUnsafe] [extTagfilter] "<xmp>"
3535
, "<ul>\n<li><input type=\"checkbox\" disabled=\"\" /> foo</li>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> bar</li>\n</ul>\n" ~=? commonmarkToHtml [] [extTaskList] "- [ ] foo\n- [x] bar"
3636
, "<p>Here is footnote<sup class=\"footnote-ref\"><a href=\"#fn-1\" id=\"fnref-1\" data-footnote-ref>1</a></sup></p>\n<section class=\"footnotes\" data-footnotes>\n<ol>\n<li id=\"fn-1\">\n<p>abc <a href=\"#fnref-1\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"1\" aria-label=\"Back to reference 1\">↩</a></p>\n</li>\n</ol>\n</section>\n" ~=? commonmarkToHtml [optFootnotes] [] "Here is footnote[^1]\n\n[^1]: abc"
37+
, Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) DOCUMENT [Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) (LIST (ListAttributes {listType = BULLET_LIST, listTight = True, listStart = 0, listDelim = PERIOD_DELIM})) [Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) (TASKLIST False) [Node (Just (PosInfo {startLine = 1, startColumn = 7, endLine = 1, endColumn = 11})) PARAGRAPH [Node (Just (PosInfo {startLine = 1, startColumn = 7, endLine = 1, endColumn = 11})) (TEXT "hello") []]]]] ~=? commonmarkToNode [] [extTaskList] "- [ ] hello"
38+
, Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) DOCUMENT [Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) (LIST (ListAttributes {listType = BULLET_LIST, listTight = True, listStart = 0, listDelim = PERIOD_DELIM})) [Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) ITEM [Node (Just (PosInfo {startLine = 1, startColumn = 3, endLine = 1, endColumn = 11})) PARAGRAPH [Node (Just (PosInfo {startLine = 1, startColumn = 3, endLine = 1, endColumn = 11})) (TEXT "[ ] hello") []]]]] ~=? commonmarkToNode [] [] "- [ ] hello"
39+
, "- [x] hello\n" ~=? nodeToCommonmark [] Nothing (Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) DOCUMENT [Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) (LIST (ListAttributes {listType = BULLET_LIST, listTight = True, listStart = 0, listDelim = PERIOD_DELIM})) [Node (Just (PosInfo {startLine = 1, startColumn = 1, endLine = 1, endColumn = 11})) (TASKLIST True) [Node (Just (PosInfo {startLine = 1, startColumn = 7, endLine = 1, endColumn = 11})) PARAGRAPH [Node (Just (PosInfo {startLine = 1, startColumn = 7, endLine = 1, endColumn = 11})) (TEXT "hello") []]]]])
3740
]
3841

0 commit comments

Comments
 (0)