Skip to content

Instantly share code, notes, and snippets.

@zenloner
Created June 22, 2013 07:36
Show Gist options
  • Save zenloner/5836222 to your computer and use it in GitHub Desktop.
Save zenloner/5836222 to your computer and use it in GitHub Desktop.
import System.Environment
import Text.Regex
import qualified Data.String.Utils as Utils
import qualified Data.Map as Map (fromList, lookup, Map, member, toList)
-- when expression
import Control.Monad
-- exitFailure
import System.Exit
import Debug.Trace
x -: f = f x
tuplify::[a]->(a, a)
tuplify [x, y, z] = (x, z)
from_maybe::Maybe String->String
from_maybe (Just x) = x
from_maybe _ = ""
transform::Regex->Map.Map String String->String->String
transform pattern dict line =
let result = matchRegex pattern line
in case result of
Just [x1, x2, x3, x4, x5]-> [x1, x2, x3, if x4=="" && Map.member x2 dict then Map.lookup x2 dict -: from_maybe else x4, x5] -: Utils.join ""
_->line
main = do
args <- getArgs
when (length args /= 3) $ do
exitFailure
-- read map from file start
content <- readFile (args !! 0)
let dict = lines content -: map (Utils.split "\t") -: filter (\x->length x == 3) -: map tuplify -: Map.fromList
-- read map from file end
Map.toList dict -: print
schema_content <- readFile (args !! 1)
let pattern = mkRegex "([ \t]*<Attribute[ \t]*name=\")(.*)(\".*type.*comment=\")(.*)(\".*)"
let write_content = lines schema_content -: map (transform pattern dict) -: unlines
writeFile (args !! 2) write_content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment