Converting anki-furigana to HTML-furigana without parser libraries I wrote a converter from anki-style furigana to HTML-style furigana. Eventually I want to do it as a parser, but as a first step I do it without. Furigana is used to write the pronunciation over certain characters in Japanese.

Example:

  


Anki would write it like this:

林[はやし]さんは 英語[えいご]は 話[はな]せます。


[]contains what's written over the char sequence in question and the char seq in question goes from the last ] to the next [. But if the next sequence begins only later (e.g. 英語), space is used to indicate that (\n works too and there is no nesting of brackets).
HTML has several ways, and I use this:

&lt;ruby&gt;&lt;rb&gt;林&lt;rt&gt;はやし&lt;/ruby&gt;さんは&lt;ruby&gt;&lt;rb&gt;英語&lt;rt&gt;えいご&lt;/ruby&gt;は&lt;ruby&gt;&lt;rb&gt;話&lt;rt&gt;はな&lt;/ruby&gt;せます。 


A sequence and its pronunciation are enclosed by &lt;ruby&gt;, &lt;rb&gt; and &lt;rt&gt; to indicate what's on top and on the bottom.


The idea is that the string first gets converted into a sequence of tags that may hold text. That sequence is then grouped and after that the tags are converted to HTML.

I'm just wondering if it is OK so far.

import System.Environment

main :: IO ()
main = do
    args &lt;- getArgs
    file &lt;- readFile (head args)
    putStrLn (anki_2_html file)

anki_2_html = translate_2_html . group . parse'

data Tag = Kana String | Uptext String
     | Furigana String String
     | Blank | Newline

--convert string to tags                                                                                   
parse' :: String -&gt; [Tag]
parse' string = parse string ""

parse :: String -&gt; String -&gt; [Tag]
parse [] []  = []
parse [] acc = [Kana acc]
parse (l:ls) ""  | l == '['  = parseUpText ls ""
                 | l == ' '  = Blank   : parse ls ""
                 | l == '\n' = Newline : parse ls ""
                 | otherwise = parse ls [l]

parse (l:ls) acc | l == '['        = Kana acc : parseUpText ls ""
                 | l `elem` " \n"  = Kana acc : parse (l:ls) ""
                 | otherwise       = parse ls (acc++[l])

parseUpText :: String -&gt; String -&gt; [Tag]
parseUpText  [] []  = []
parseUpText  [] acc = [Kana (' ':acc)]
parseUpText  (l:ls) []  | l == ']'  = parse  ls ""
                        | otherwise = parseUpText ls [l]

parseUpText  (l:ls) acc | l == ']'   = Uptext acc : parse  ls ""
                        | otherwise = parseUpText ls (acc++[l])

--group patterns of Tags                                                                                   
group :: [Tag] -&gt; [Tag]
group (Blank   : Kana kj : Uptext ka : ms) = Furigana kj ka : group ms
group           (Kana kj : Uptext ka : ms) = Furigana kj ka : group ms
group (m:ms) = m : group ms
group [] = []

translate_2_html ::  [Tag] -&gt; String
translate_2_html = foldl1 (++) . map m2s
          where
            m2s (Furigana below above) = "&lt;ruby&gt;&lt;rb&gt;"++below++"&lt;rt&gt;"++above++"&lt;/ruby&gt;"
            m2s (Kana k) = k
            m2s (Uptext f) = "&lt;f&gt;" ++ f ++ "&lt;/f&gt;"
            m2s Blank  = " "
            m2s Newline= "&lt;br&gt;\n"
Building a list like ((... ++ [_]) ++ [_]) ++ [_] takes quadratic time, break doesn't.

parse :: String -&gt; [Tag]
parse lss = case break (`elem` "[ \n") lss of
  ([],         []) -&gt; []
  ([],     '[':ls) -&gt; parseUpText ls
  ([],     ' ':ls) -&gt; Blank     : parse ls
  ([],    '\n':ls) -&gt; Newline   : parse ls
  (kana,       ls) -&gt; Kana kana : parse ls

parseUpText :: String -&gt; [Tag]
parseUpText lss = case break (== ']') lss of
  ([],         []) -&gt; []
  (kana,       []) -&gt; [Kana (' ':kana)]
  ([],     ']':ls) -&gt; parse ls
  (uptext, ']':ls) -&gt; Uptext uptext : parse ls