您好,我正在尝试编写一个将markdown转换为HTML的程序,我知道有Pandoc,但是我的项目是手动将其写出。我已经完成了,或者至少我认为我lexical error in string/character literal at character ' \r'
愿意,但是我遇到了以下错误Haskell错误对于所指的内容我一无所知,指出它的任何帮助都会很棒。谢谢,所以更新了:我用其他符号更改了一些内容,现在得到的错误是hs.38:17: Not in scope '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 = "" convertToHTML :: String -> String convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---"
" 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 = "" ++ (tail $ tail x) | isPrefixOf "__" x = "" ++ (tail $ tail x) | isPrefixOf "_" x = "" ++ (tail x) | isPrefixOf "*" x = "" ++ (tail x) | isPrefixOf "^" x = "" ++ (tail x) | isPrefixOf "---" x = replace "---" "
" | 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 "**" "" x | isInfixOf "__" x = replace "__" "" x | isInfixOf "_" x = replace "_" "" x | isInfixOf "*" x = replace "*" "" x | isInfixOf "^" x = replace "^" "" x | isInfixOf "---" x = replace "---" "
" x | otherwise = x ch = splitOn " " str headings str = unlines $ map heads (lines str) where heads x | isPrefixOf "######" x = "" ++ (numTail 6 x) ++ "
" | isPrefixOf "#####" x = "" ++ (numTail 5 x) ++ "
" | isPrefixOf "####" x = "" ++ (numTail 4 x) ++ "
" | isPrefixOf "###" x = "" ++ (numTail 3 x) ++ "
" | isPrefixOf "##" x = "" ++ (tail $ tail x) ++ "
" | isPrefixOf "#" x = "" ++ (tail x) ++ "
" | otherwise = x specialTags str = unlines $ map tags (lines str) where tags x | isPrefixOf "[code]" x = "" | 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 = " " ++ "" ++ "" ++ (numTail 6 x) | isSuffixOf "[code]" x = (numInit 6 x) ++ "" ++title++" " ++"" writeFile outfile $ convertToHTML $ header ++ convertSpecialChar contentlines
Stefan Holde.. 5
在第17行,您缺少双引号。我想replace "---"
应该读
" xreplace "---" "
。
" x
函数的整个声明convertToHTML
将读取为
convertToHTML :: String -> String convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---" "
" x
'\r'
呢?编译Haskell模块的第一阶段是词法分析,其中将程序文本分解为标记,然后由解析器对其进行处理。在您的情况下,词法分析失败,因为假设新的字符串文字由开头" x
。然后,它遇到了第17行的结尾,而文字却没有正确地以结束的双引号或任何表示字符串文字为多行字符串文字的指示正确终止。由于这是非法的词法语法,因此它抱怨遇到的行尾('\r'
)。
诚然,如果错误消息明确提到了一个非终止的字符串文字,那将会更有帮助。
无论如何,对于Haskell具有语法高亮显示支持的编辑器很可能早就暗示了这个问题。;)
然后,该变量str
在您的本地定义中不在范围内的问题 ch
是由于布局。确保ch
缩进与的先前定义相同的级别tag
。也就是说,代替
beginnings :: String -> String beginnings str = unwords $ map tag ch where tag x | isPrefixOf "**" x = "" ++ (tail $ tail x) | isPrefixOf "__" x = "" ++ (tail $ tail x) | isPrefixOf "_" x = "" ++ (tail x) | isPrefixOf "*" x = "" ++ (tail x) | isPrefixOf "^" x = "" ++ (tail x) | isPrefixOf "---" x = replace "---" "
" | otherwise = x ch =splitOn " " str
你应该写类似
beginnings :: String -> String beginnings str = unwords $ map tag ch where tag x | isPrefixOf "**" x = "" ++ (tail $ tail x) | isPrefixOf "__" x = "" ++ (tail $ tail x) | isPrefixOf "_" x = "" ++ (tail x) | isPrefixOf "*" x = "" ++ (tail x) | isPrefixOf "^" x = "" ++ (tail x) | isPrefixOf "---" x = replace "---" "
" | otherwise = x ch = splitOn " " str
请记住,在Haskell中,代码的布局很重要。
在第17行,您缺少双引号。我想replace "---"<hr>" x
应该读replace "---" "<hr>" x
。
函数的整个声明convertToHTML
将读取为
convertToHTML :: String -> String convertToHTML x = specialTags $ headings $ endings $ beginnings $ replace "---" "<hr>" x
'\r'
呢?编译Haskell模块的第一阶段是词法分析,其中将程序文本分解为标记,然后由解析器对其进行处理。在您的情况下,词法分析失败,因为假设新的字符串文字由开头" x
。然后,它遇到了第17行的结尾,而文字却没有正确地以结束的双引号或任何表示字符串文字为多行字符串文字的指示正确终止。由于这是非法的词法语法,因此它抱怨遇到的行尾('\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中,代码的布局很重要。