LoginSignup
1
0

More than 5 years have passed since last update.

pukiwikiのwikiデータのファイル名を読みやすい構造に変換する

Posted at
trans_pukiwiki_fnames.ml
(*
  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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0