Advertisement
Guest User

Untitled

a guest
Apr 1st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env runhaskell
  2.  
  3. {-# LANGUAGE OverloadedStrings #-}
  4.  
  5. --import Text.Pandoc.Generic
  6. --import Text.Pandoc.Definition
  7. import Text.Pandoc --as P(readerExtensions, multimarkdownExtensions, def, readMarkdown, runIOorExplode)
  8. import Numeric
  9. import Data.Text(Text, unpack, pack, intercalate)
  10. import Data.Char(ord)
  11. import Text.Regex.TDFA
  12. import qualified Data.Text.IO as T
  13.  
  14. mmdOutwardLinks :: Text -> IO Text
  15. mmdOutwardLinks txt = runIOorExplode $
  16.     readMarkdown def{ readerExtensions=multimarkdownExtensions } txt
  17.     >>= getLinks
  18.  
  19. main :: IO ()
  20. main = do
  21.     T.getContents >>= mmdOutwardLinks >>= T.putStrLn
  22.  
  23.  
  24.  
  25. digit36 :: Char -> Int
  26. digit36 x | x `elem` ['0'..'9'] = (ord x) - (ord '0')
  27.           | x `elem` ['A'..'Z'] = (ord x) - (ord 'A')  + 10
  28.           | x `elem` ['a'..'z'] = (ord x) - (ord 'a')  + 10
  29.  
  30. base36 :: String -> Int
  31. base36 x = fst $ head $ readInt 36 (`elem` ['0'..'9']++['a'..'z']++['A'..'Z']) digit36 x
  32.  
  33. extractLink :: String -> [Int]
  34. extractLink x | x =~ decRegex :: Bool = map base10 $ getMatches $ getResult decRegex x
  35.               | x =~ b36Regex :: Bool = map base36 $ getMatches $ getResult b36Regex x
  36.               | otherwise = []
  37.     where b36Regex = "^\\[\\[([0-9A-Za-z]{8})\\]\\]$"
  38.           decRegex = "^\\[\\[([0-9]{12})\\]\\]$"
  39.           getMatches (_, _, _, xs) = xs
  40.           getResult :: String -> String -> (String, String, String, [String])
  41.           getResult regex var = var =~ regex
  42.           base10 x = read x :: Int
  43.  
  44. getLink :: Inline -> [Int]
  45. getLink (Str txt) = extractLink $ unpack txt
  46. getLink _ = []
  47.  
  48. getLinks :: Pandoc -> Text
  49. getLinks p = intercalate " " $ map (pack.show) $ queryWith getLink p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement