안녕하세요 저는 마크 다운을 HTML로 변환하는 프로그램을 작성하려고합니다. Pandoc이 있다는 것을 이해하지만 프로젝트가 수동으로 작성하고 있습니다. 나는 그것을 완료했거나 적어도 나는 생각하지만 다음과 같은 오류가 발생합니다. Haskell 오류 lexical error in string/character literal at character ' \r'
나는 무엇을 말하는지에 대한 아이디어가 없으며 그것을 지적하는 데 도움이 될 것입니다. 감사합니다 그래서 업데이트 : 나는 기타 기호로 몇 가지를 변경했으며 지금 얻는 오류는 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 = "</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)의 끝을 발견 여러 줄 문자열 리터럴 . 이것은 잘못된 어휘 구문이므로 발생한 줄 끝 ( '\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에서는 코드 레이아웃이 중요 합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다