您好,我正在尝试编写一个将 markdown 转换为 HTML 的程序,我知道有 Pandoc,但我的项目正在手动编写它。我已经完成了,或者至少我认为我已经完成了,但我收到以下错误 Haskell Error lexical error in string/character Lite at character '\r'
我不知道所指的是什么到,任何指出这一点的帮助都会很棒。谢谢所以更新:我用杂项符号更改了一些内容,现在得到的错误是 hs.38:17: Not in range 'str'
并且当我正在处理它反复保留的内容时指向第 38 行,我无法弄清楚问题是什么,因为它忽略了上一个函数中的相同内容
module Main
(
convertToHTML,
convertSpecialChar,
main
) where
import System.Environment (getArgs)
import System.IO
import Data.Char
import Data.List
import Data.List.Split
eof = "</body></html>"
convertToHTML :: String -> String
convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---"<hr>" x
convertSpecialChar :: String -> String
ConvertSpecialChar x = (convertLessThan $ convertAmpersand $ convertGreaterThan x)++eof
where
convertLessThan str = concat [if c =='<' then '<" else [c] | c <- str]
convertAmpersand str = concat [if c == '&' then "&" else [c] | c <- str]
convertGreaterThan str = concat [if c =='>' then ">" else [c] | c <- str]
beginnings :: String -> String
beginnings str = unwords $ map tag ch
where
tag x
| isPrefixOf "**" x = "<strong>" ++ (tail $ tail x)
| isPrefixOf "__" x = "<strong>" ++ (tail $ tail x)
| isPrefixOf "_" x = "<em>" ++ (tail x)
| isPrefixOf "*" x = "<em>" ++ (tail x)
| isPrefixOf "^" x = "<p>" ++ (tail x)
| isPrefixOf "---" x = replace "---" "<hr>"
| otherwise = x
ch =splitOn " " str
replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace old new x = intercalate new (splitOn old x)
endings :: String -> String
endings str = unwords $ map tag ch
where
tag x
| isInfixOf "**" x = replace "**" "</strong>" x
| isInfixOf "__" x = replace "__" "</strong>" x
| isInfixOf "_" x = replace "_" "</em>" x
| isInfixOf "*" x = replace "*" "</em>" x
| isInfixOf "^" x = replace "^" "</p>" x
| isInfixOf "---" x = replace "---" "<hr>" x
| otherwise = x
ch = splitOn " " str
headings str = unlines $ map heads (lines str)
where
heads x
| isPrefixOf "######" x = "<h6>" ++ (numTail 6 x) ++ "</h6>"
| isPrefixOf "#####" x = "<h5>" ++ (numTail 5 x) ++ "</h5>"
| isPrefixOf "####" x = "<h4>" ++ (numTail 4 x) ++ "</h4>"
| isPrefixOf "###" x = "<h3>" ++ (numTail 3 x) ++ "</h3>"
| isPrefixOf "##" x = "<h2>" ++ (tail $ tail x) ++ "</h2>"
| isPrefixOf "#" x = "<h1>" ++ (tail x) ++ "</h1>"
| otherwise = x
specialTags str = unlines $ map tags (lines str)
where
tags x
| isPrefixOf "[code]" x = "<blockquote><pre><code>" ++ (numTail 6 x)
| isSuffixOf "[code]" x = (numInit 6 x) ++ "</code></pre></blockquote>"
| otherwise = x
numTail :: Int -> String -> String
numTail _ [] = []
numTail 1 str = tail str
numTail x str = tail $ (numTail (x-1) str)
numInit :: Int-> String -> String
numInit _ [] = []
numInit 1 str = init str
numInit x str = init $ (numInit (x-1) str)
main = do
args <- getArgs
let (infile,outfile) = (\\(x:y:ys) -> (x,y)) args
putStrLn $ "Input file: " ++ infile
putStrLn $ "Output file: " ++ outfile
contents <- readFile infile
let contentlines = unlines $ tail $ lines contents
let title = head $ lines contents
let header = "<!DOCTYPE html> <head>" ++ "<meta http-equiv = \\"Content-type\\"content=\\"text/html; charset=utf-8\\" />" ++ "<title>" ++title++"</title>" ++"</head><body>"
writeFile outfile $ convertToHTML $ header ++ convertSpecialChar contentlines
最佳答案
在第 17 行,您缺少双引号。我想replace "---"<hr>" x
应该读 replace "---" "<hr>" x
.
函数的完整声明convertToHTML
然后将阅读
convertToHTML :: String -> String
convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---" "<hr>" x
那么为什么编译器会提示字符 '\r'
?
编译 Haskell 模块的第一阶段是词法分析,其中程序文本被分解为标记,然后由解析器处理。在您的情况下,词法分析失败,因为假设新的字符串文字是由 " x
开始的。 。然后它遇到了第 17 行的结尾,而文字开始没有用右双引号正确终止,也没有任何字符串文字为 multiline string literal 的指示。 。由于这是非法的词法语法,因此它会提示遇到行尾( '\r'
)。
诚然,如果错误消息明确提及非终止字符串文字,则会更有帮助。
无论如何,一个支持 Haskell 语法高亮的编辑器可能早就暗示了这个问题。 ;)
缩进
您遇到的变量 str
的问题不在您本地定义 ch
的范围内是由于布局。确保ch
与 tag
的前面定义处于同一级别缩进。 。也就是说,而不是
beginnings :: String -> String
beginnings str = unwords $ map tag ch
where
tag x
| isPrefixOf "**" x = "<strong>" ++ (tail $ tail x)
| isPrefixOf "__" x = "<strong>" ++ (tail $ tail x)
| isPrefixOf "_" x = "<em>" ++ (tail x)
| isPrefixOf "*" x = "<em>" ++ (tail x)
| isPrefixOf "^" x = "<p>" ++ (tail x)
| isPrefixOf "---" x = replace "---" "<hr>"
| otherwise = x
ch =splitOn " " str
你应该写一些类似的内容
beginnings :: String -> String
beginnings str = unwords $ map tag ch
where
tag x
| isPrefixOf "**" x = "<strong>" ++ (tail $ tail x)
| isPrefixOf "__" x = "<strong>" ++ (tail $ tail x)
| isPrefixOf "_" x = "<em>" ++ (tail x)
| isPrefixOf "*" x = "<em>" ++ (tail x)
| isPrefixOf "^" x = "<p>" ++ (tail x)
| isPrefixOf "---" x = replace "---" "<hr>"
| otherwise = x
ch = splitOn " " str
请记住,在 Haskell 中,the layout of your code matters .
关于Haskell Error 字符 '\r' 处字符串/字 rune 字中的词法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27329771/