- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在尝试编写一个将 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/
package main var lettersLower = []rune("abcdefghijklmnopqrstuvwxyz") var lettersUpper = []rune("ABCD
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
我正在尝试使用 utf8 十六进制代码生成连续字符。例如第一部分,我称之为“基本”代码:259第二部分,我称之为“结束”代码:1(或,2,或 A,或 F,等等) 这些以字符串的形式出现。一旦我附加了结
我有一个字符串 MyString我想在这个数据中附加如下内容: MYString ("1", "a"), ("1", "b") //END result 我的代码是这样的: query :=
这是我的代码段: var converter = map[rune]rune {//some data} sample := "⌘こんにちは" var tmp string for _, runeVa
根据https://blog.golang.org/strings在我的测试中,看起来当我们 range 一个字符串时,我们得到的字符是 rune 类型,但是如果我们通过 str[index] 获取它
我有一个非常非常大的文本文件(比内存大得多)。我想做的是使用类似于: for record in myFile: process_record(); 添加的技巧是我的记录由空行分隔(中间有各种东西)
我想连接一个字符串文字和一个字 rune 字。由于语法不正确,"abc"'d' "efg" 会导致编译器错误: x.c:4:24: error: expected ',' or ';' before
我想连接一个字符串文字和一个字 rune 字。由于语法不正确,"abc"'d' "efg" 会导致编译器错误: x.c:4:24: error: expected ',' or ';' before
我在网上看到一个使用 golang 中的 rune() 函数的函数,但我很难找到它是什么。我正在阅读教程并且对文档没有经验,所以很难找到我要找的东西。 具体来说,我想看看为什么会失败...... fm
我正在查看 string.Map 函数,它必须采用返回 rune 的映射函数。我想通过调用来消除解析为 false 的 rune :unicode.IsPrint() func Map(映射 func
我在按字符排序字符串时遇到问题(要检查两个字符串是否是字谜,我想对它们都进行排序,并检查是否相等)。 我可以像这样得到字符串 s 的 []rune 表示: runes := make([]rune,
什么是rune在围棋? 我一直在谷歌搜索,但 Golang 只说一行: rune是 int32 的别名. 但是整数怎么会像交换案例一样到处使用呢? 下面是一个函数swapcase。什么是<=和 - ?
我正在使用具有此约束规则的泛型: type LineParser[T []rune | string] struct { } 我有那个结构的通用方法: func (it *LineParser[T])
我有一个包含波兰语字符的 csv,但是当我在 SAS 中导入时,某些波兰语字符被替换为“?”或任何其他随机变量,我该如何处理。我有一个所有可能的波兰语字符的列表,我不介意它被它的英语对应字符取代 最佳
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
有点像 const X: char = '0x10FFFC'; 最佳答案 是的,使用\u{..}: const X: char = '\u{10FFFC}'; Playground 这种情况下的一个技
我有一个名为“ roケールストリングのキャッシュ最适化-v3-20160202_1047.pdf”的pdf文件。 使用Google标记管理器从下载URL获取文件名时,我得到的值为“%E4%BE%9B
程序目的: rune 密码 最终编辑: 我现在(感谢 非常有用的 由 Extremely 提供的答案 了不起的人 )完成了我一直在从事的项目;并且 - 对于 future 的读者,我还提供了完整的代码
如何在Scala中为垂直制表符('\v',ASCII 11)编写字 rune 字? '\v'不起作用。 (无效的转义字符)'\11'应该是,但是... scala> '\11'.toInt res13
我是一名优秀的程序员,十分优秀!