git.iko.soy/typeable/pandoc
on master help sign in
Muse: add RTL support

Closes #5551

commit0713cb65bc6fcea0c950d1afcdfdce412a8b56d4 authorAlexander Krotov <ilabdsf@gmail.com> dateSat 13 Jul 2019, 20:54 (7 years ago) parentf6c92c7
4 files changed+31−5
src/Text/Pandoc/Readers/Muse.hs+12 src/Text/Pandoc/Writers/Muse.hs+9−5 test/Tests/Readers/Muse.hs+3 test/Tests/Writers/Muse.hs+7
src/Text/Pandoc/Readers/Muse.hs+12
@@ -746,6 +746,8 @@ inline' = whitespace
746746 <|> strikeoutTag
747747 <|> verbatimTag
748748 <|> classTag
749+ <|> inlineRtl
750+ <|> inlineLtr
749751 <|> nbsp
750752 <|> linkOrImage
751753 <|> code
@@ -863,6 +865,16 @@ classTag = do
863865 classes <- maybe [] words . lookup "name" <$> openTag "class"
864866 fmap (B.spanWith ("", classes, [])) . mconcat <$> manyTill inline (closeTag "class")
865867
868+-- | Parse @\<\<\<RTL>>>@ text.
869+inlineRtl :: PandocMonad m => MuseParser m (F Inlines)
870+inlineRtl = try $
871+ fmap (B.spanWith ("", [], [("dir", "rtl")])) . mconcat <$ string "<<<" <*> manyTill inline (string ">>>")
872+
873+-- | Parse @\<\<\<LTR>>>@ text.
874+inlineLtr :: PandocMonad m => MuseParser m (F Inlines)
875+inlineLtr = try $
876+ fmap (B.spanWith ("", [], [("dir", "ltr")])) . mconcat <$ string ">>>" <*> manyTill inline (string "<<<")
877+
866878-- | Parse "~~" as nonbreaking space.
867879nbsp :: PandocMonad m => MuseParser m (F Inlines)
868880nbsp = try $ pure (B.str "\160") <$ string "~~"
src/Text/Pandoc/Writers/Muse.hs+9−5
@@ -377,6 +377,7 @@ shouldEscapeString s = do
377377 "::" `isInfixOf` s ||
378378 "~~" `isInfixOf` s ||
379379 "[[" `isInfixOf` s ||
380+ ">>>" `isInfixOf` s ||
380381 ("]" `isInfixOf` s && insideLink) ||
381382 containsNotes '[' ']' s ||
382383 containsNotes '{' '}' s
@@ -412,7 +413,7 @@ removeKeyValues :: Inline -> Inline
412413removeKeyValues (Code (i, cls, _) xs) = Code (i, cls, []) xs
413414-- Do not remove attributes from Link
414415-- Do not remove attributes, such as "width", from Image
415−removeKeyValues (Span (i, cls, _) xs) = Span (i, cls, []) xs
416+-- Do not remove attributes, such as "dir", from Span
416417removeKeyValues x = x
417418
418419normalizeInlineList :: [Inline] -> [Inline]
@@ -682,14 +683,17 @@ inlineToMuse (Note contents) = do
682683 n <- gets stNoteNum
683684 let ref = show $ n + length notes
684685 return $ "[" <> text ref <> "]"
685−inlineToMuse (Span (anchor,names,_) inlines) = do
686+inlineToMuse (Span (anchor,names,kvs) inlines) = do
686687 contents <- inlineListToMuse inlines
688+ let (contents', hasDir) = case lookup "dir" kvs of
689+ Just "rtl" -> ("<<<" <> contents <> ">>>", True)
690+ Just "ltr" -> (">>>" <> contents <> "<<<", True)
691+ _ -> (contents, False)
687692 let anchorDoc = if null anchor
688693 then mempty
689694 else text ('#':anchor) <> space
690695 modify $ \st -> st { stUseTags = False }
691696 return $ anchorDoc <> (if null inlines && not (null anchor)
692697 then mempty
693− else (if null names
694− then "<class>"
695− else "<class name=\"" <> text (head names) <> "\">") <> contents <> "</class>")
698+ else (if null names then (if hasDir then contents' else "<class>" <> contents' <> "</class>")
699+ else "<class name=\"" <> text (head names) <> "\">" <> contents' <> "</class>"))
test/Tests/Readers/Muse.hs+3
@@ -236,6 +236,9 @@ tests =
236236 , "Class tag" =: "<class name=\"foo\">bar</class>" =?> para (spanWith ("", ["foo"], []) "bar")
237237 , "Class tag without name" =: "<class>foobar</class>" =?> para (spanWith ("", [], []) "foobar")
238238
239+ , "RTL" =: "<<<foo bar>>>" =?> para (spanWith ("", [], [("dir", "rtl")]) "foo bar")
240+ , "LTR" =: ">>>foo bar<<<" =?> para (spanWith ("", [], [("dir", "ltr")]) "foo bar")
241+
239242 -- <em> tag should match with the last </em> tag, not verbatim one
240243 , "Nested \"</em>\" inside em tag" =: "<em>foo<verbatim></em></verbatim>bar</em>" =?> para (emph "foo</em>bar")
241244
test/Tests/Writers/Muse.hs+7
@@ -620,6 +620,13 @@ tests = [ testGroup "block elements"
620620 , "adjacent spans" =: spanWith ("", ["syllable"], []) (str "wa") <>
621621 spanWith ("", ["syllable"], []) (str "ter")
622622 =?> "<class name=\"syllable\">wa</class><class name=\"syllable\">ter</class>"
623+ , testGroup "RTL"
624+ [ "RTL span" =: spanWith ("",[],[("dir", "rtl")]) (text "foo bar") =?> "<<<foo bar>>>"
625+ , "LTR span" =: spanWith ("",[],[("dir", "ltr")]) (text "foo bar") =?> ">>>foo bar<<<"
626+ , "RTL span with a class" =: spanWith ("",["foobar"],[("dir", "rtl")]) (text "foo bar") =?> "<class name=\"foobar\"><<<foo bar>>></class>"
627+ , "LTR span with a class" =: spanWith ("",["foobar"],[("dir", "ltr")]) (text "foo bar") =?> "<class name=\"foobar\">>>>foo bar<<<</class>"
628+ , "Escape <<< and >>>" =: plain (text "<<< foo bar >>>") =?> "<verbatim><<<</verbatim> foo bar <verbatim>>>></verbatim>"
629+ ]
623630 , testGroup "combined"
624631 [ "emph word before" =:
625632 para ("foo" <> emph "bar") =?>