Skip to content

Commit 619aa93

Browse files
committed
Add escaped characters
1 parent d77d6bf commit 619aa93

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Here, I have used the `neorg-pandoc-linux86` binary to transform my `norg` file
6060
- @image :x:
6161
- Custom :x:
6262
- Trailing modifiers :heavy_check_mark:
63-
- Escaping :x:
63+
- Escaping :heavy_check_mark:
6464
- Horizontal line :heavy_check_mark:
6565
- Links :x:
6666

src/Neorg/Parser.hs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,14 @@ paragraph' end' = do
289289
modify $ modifierInline .~ new
290290
where
291291
close (NoModifier b) = fail "No closing"
292-
close (OpenModifier cm i b) =
292+
close (OpenModifier cm i b) =
293293
case b of
294294
(OpenModifier cd id bd) -> if c == cm then pure (OpenModifier cd (id <> f i) bd) else close (OpenModifier cd (id <> Text cm <> i) bd)
295295
(NoModifier id) -> if c == cm then pure (NoModifier (id <> f i)) else fail "No closing"
296296
pushStack :: Text -> StateT InlineState p ()
297297
pushStack c = do
298298
s <- gets (view modifierInline)
299-
new <- case s of
299+
new <- case s of
300300
NoModifier i -> pure $ OpenModifier c mempty (NoModifier i)
301301
stack@(OpenModifier cm i b) ->
302302
if not $ hasModifier c stack
@@ -337,8 +337,8 @@ paragraph' end' = do
337337
where
338338
follow =
339339
singleSpace <|> newline
340-
<|> withNextChar
341-
(guard . flip S.member (punctuationSymbols <> attachedModifierSymbols))
340+
<|> withNextChar
341+
(guard . flip S.member (punctuationSymbols <> attachedModifierSymbols))
342342
parseOpening c = do
343343
P.try $ do
344344
anyChar >> withNextChar (\c -> guard $ isLetter c || S.member c specialSymbols)
@@ -374,17 +374,31 @@ paragraph' end' = do
374374
modify (delimitedActive .~ False)
375375
if S.member c (punctuationSymbols <> attachedModifierSymbols)
376376
then
377-
( do
378-
p <- lift anyChar <&> pack . (: [])
379-
appendInlineToStack (Text p)
380-
withNextChar $ \c -> parWhitespace c <|> attachedClosings c <|> attachedOpenings c <|> word c
377+
( P.try
378+
( do
379+
guard (c == '\\')
380+
a <- anyChar >> anyChar
381+
guard (a > ' ')
382+
pure a
383+
)
384+
>>= \x -> do
385+
appendInlineToStack
386+
(Text $ pack [x])
387+
withNextChar $
388+
\c -> parWhitespace c <|> attachedClosings c <|> word c
381389
)
390+
<|> ( do
391+
p <- lift anyChar <&> pack . (: [])
392+
appendInlineToStack (Text p)
393+
withNextChar $ \c -> parWhitespace c <|> attachedClosings c <|> attachedOpenings c <|> word c
394+
)
382395
else
383396
( do
384397
w <- P.takeWhile1P (Just "Word") (\c -> c > ' ' && S.notMember c (punctuationSymbols <> attachedModifierSymbols))
385398
appendInlineToStack (Text w)
386399
withNextChar $ \c -> parWhitespace c <|> attachedClosings c <|> word c
387400
)
401+
388402
punctuationSymbols = S.fromList "?!:;,.<>()[]{}'\"/#%&$£€-*\\~"
389403
attachedModifierSymbols = S.fromList "*/_-^,|`$="
390404

@@ -396,7 +410,7 @@ paragraph' end' = do
396410
intersectingModifier :: Char -> StateT InlineState p ()
397411
intersectingModifier c1 = do
398412
c2 <- followedBy $ anyChar >> anyChar
399-
case c1:[c2] of
413+
case c1 : [c2] of
400414
":*" -> intersectingOpen ":*"
401415
":/" -> intersectingOpen ":/"
402416
":_" -> intersectingOpen ":_"
@@ -406,7 +420,6 @@ paragraph' end' = do
406420
":|" -> intersectingOpen ":|"
407421
":`" -> parseTextModifier (pure ()) ":`" Verbatim
408422
":$" -> parseTextModifier (pure ()) ":$" Math
409-
410423
"*:" -> intersectingClosed ":*" Bold
411424
"/:" -> intersectingClosed ":/" Italic
412425
"_:" -> intersectingClosed ":_" Underline

test/Parser.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ paragraphTests =
133133
testCase "Single-Line intersecting Subscript" $ parse singleLineParagraph ":,sub,:" @?= Subscript (Text "sub"),
134134
testCase "Single-Line intersecting Spoiler" $ parse singleLineParagraph ":|spoiler|:" @?= Spoiler (Text "spoiler"),
135135
testCase "Single-Line intersecting Math" $ parse singleLineParagraph ":$math$:" @?= Math "math",
136-
testCase "Single-Line intersecting Verbatim" $ parse singleLineParagraph ":`verbatim`:" @?= Verbatim "verbatim"
136+
testCase "Single-Line intersecting Verbatim" $ parse singleLineParagraph ":`verbatim`:" @?= Verbatim "verbatim",
137+
testCase "Escape bold character" $ parse singleLineParagraph "\\*test*" @?= Text "*test*"
137138
]
138139

139140
markerTests :: TestTree
@@ -235,7 +236,14 @@ listTests =
235236
V.singleton $ ListParagraph (Text "l2")
236237
]
237238
}
238-
]
239+
],
240+
testCase "Escaped list" $
241+
parse (blocks @Empty) "\\- test1"
242+
@?= V.singleton
243+
( Paragraph
244+
( ConcatInline $ V.fromList [Text "-", Space, Text "test1"]
245+
)
246+
)
239247
]
240248

241249
headingTests :: TestTree

0 commit comments

Comments
 (0)