Created
December 10, 2012 07:27
-
-
Save yoshihiro503/4249032 to your computer and use it in GitHub Desktop.
pukiwikiのwikiデータのファイル名を読みやすい構造に変換する ref: http://qiita.com/items/25d3ac45603ae1090254
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
pukiwikiの内部データのファイル名を可読できる名前に変換する | |
ls /PUKIWIKIDIR/wiki/*.txt | ocaml trans_pukiwiki_fnames.ml | |
*) | |
let outdir = "out/" | |
let verbose = true | |
let (|>) x f = f x | |
let (@@) f x = f x | |
let (!%) = Printf.sprintf | |
let rec split_at n xs = | |
match (n, xs) with | |
| (0, xs) -> ([],xs) | |
| (n, []) -> ([], []) | |
| (n, x::xs) -> | |
let (ys,zs) = split_at (n-1) xs in | |
(x::ys, zs) | |
let rec group n xs = | |
match split_at n xs with | |
| (ys, []) -> [ys] | |
| (ys,zs) -> ys :: group n zs | |
let implode cs = | |
String.concat "" (List.map (String.make 1) cs) | |
let explode s = | |
let rec explode_rec n = | |
if n >= String.length s then | |
[] | |
else | |
String.get s n :: explode_rec (succ n) | |
in | |
explode_rec 0 | |
let trans1 cs = | |
let hex char = | |
match char with | |
| '0'..'9' -> int_of_char char - int_of_char '0' | |
| 'a'..'f' -> 10 + int_of_char char - int_of_char 'a' | |
| 'A'..'F' -> 10 + int_of_char char - int_of_char 'A' | |
| _ -> failwith (!%"trans1: %c" char) | |
in | |
List.map hex cs | |
|> List.fold_left (fun x y -> 16*x+y) 0 | |
|> char_of_int | |
let trans s = | |
String.sub s 0 (String.length s - 4) | |
|> explode | |
|> group 2 | |
|> List.map trans1 | |
|> implode | |
|> (fun s -> s ^ ".txt") | |
let read_lines () = | |
let rec iter store = | |
try iter (read_line () :: store) with | |
| End_of_file -> List.rev store | |
in | |
iter [] | |
let command s = | |
if verbose then print_endline s; | |
Sys.command s | |
let mkdir_p filepath = | |
command ("mkdir -p " ^ filepath) | |
let cp x y = | |
command ("cp '"^x^"' '"^y^"'") | |
let _work path = | |
let basename = Filename.basename path in | |
let new_path = trans basename in (* 変換後のパス ('/' が含まれる!) *) | |
let dstdir = outdir ^ Filename.dirname new_path in | |
ignore @@ mkdir_p dstdir; | |
ignore @@ cp path (outdir ^"/"^ new_path); | |
() | |
let work path = | |
try _work path with | |
| e -> | |
prerr_endline ("work error: "^path); | |
raise e | |
let () = | |
read_lines () | |
|> List.iter work |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment