Parcourir la source

First version of ascii->utf8 code plus some test files

Taddeus Kroes il y a 11 ans
commit
97dfaabb93
6 fichiers modifiés avec 264 ajouts et 0 suppressions
  1. 3 0
      .gitignore
  2. 8 0
      Makefile
  3. 204 0
      artify.ml
  4. 13 0
      graph.txt
  5. 18 0
      simple.txt
  6. 18 0
      table.txt

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+artify
+*.cmi
+*.cmx

+ 8 - 0
Makefile

@@ -0,0 +1,8 @@
+.PHONY: clean
+
+artify: artify.ml
+	ocamlfind ocamlopt -linkpkg -package camomile -package str -o $@ -g $<
+	rm -f $@.cmi $@.cmx $@.o
+
+clean:
+	rm -f artify *.cmi *.cmx

+ 204 - 0
artify.ml

@@ -0,0 +1,204 @@
+open CamomileLibrary
+
+let input_all ic =
+  let bufsize = 512 in
+  let buf = Bytes.create bufsize in
+  let rec loop cur =
+    match input ic buf 0 bufsize with
+    | 0 -> close_in ic; cur
+    | nread -> loop (cur ^ Bytes.sub_string buf 0 nread)
+  in
+  loop ""
+
+let pad_right s =
+  let rec repeat s n = if n < 1 then "" else s ^ (repeat s (n - 1)) in
+  let lines = Str.split (Str.regexp " *\n") s in
+  let cmp_length max line =
+    let l = String.length line in
+    if l > max then l else max
+  in
+  let w = List.fold_left cmp_length 0 lines in
+  let pad buf line = buf ^ line ^ repeat " " (w - String.length line + 1) ^ "\n" in
+  List.fold_left pad "" lines
+
+let artify replace_pat s =
+  let w = try String.index s '\n' + 1 with Not_found -> String.length s in
+  let h = String.length s / w in
+  let char_at x y =
+    if x < 0 || y < 0 || x >= w || y >= h
+      then ' '
+      else String.get s (y * w + x)
+  in
+  let make_pat i =
+    let x = i mod w in
+    let y = i / w in
+    let o ox oy = char_at (x + ox) (y + oy) in
+    let m1 = -1 in
+    [|o m1 m1; o 0 m1; o 1 m1;
+      o m1  0; o 0  0; o 1  0;
+      o m1  1; o 0  1; o 1  1|]
+  in
+  UTF8.init (String.length s) (fun i -> replace_pat (make_pat i))
+
+let u s = UTF8.get s 0
+
+let replace_pat_safe = function
+  | [|   _   ; '|'|'+';    _   ;
+      '-'|'+';   '+'  ; '-'|'+';
+         _   ; '|'|'+';    _   |] -> u "┼"
+  | [|   _   ;    _   ;    _   ;
+      '-'|'+';   '+'  ; '-'|'+';
+         _   ; '|'|'+';    _   |] -> u "┬"
+  | [|   _   ; '|'|'+';    _   ;
+      '-'|'+';   '+'  ; '-'|'+';
+         _   ;    _   ;    _   |] -> u "┴"
+  | [|   _   ; '|'|'+';    _   ;
+         _   ;   '+'  ; '-'|'+';
+         _   ; '|'|'+';    _   |] -> u "├"
+  | [|   _   ; '|'|'+';    _   ;
+      '-'|'+';   '+'  ;    _   ;
+         _   ; '|'|'+';    _   |] -> u "┤"
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '+'  ; '-'|'+';
+         _   ; '|'|'+';    _   |] -> u "┌"
+  | [|   _   ; '|'|'+';    _   ;
+         _   ;   '+'  ; '-'|'+';
+         _   ;    _   ;    _   |] -> u "└"
+  | [|   _   ;    _   ;    _   ;
+      '-'|'+';   '+'  ;    _   ;
+         _   ; '|'|'+';    _   |] -> u "┐"
+  | [|   _   ; '|'|'+';    _   ;
+      '-'|'+';   '+'  ;    _   ;
+         _   ;    _   ;    _   |] -> u "┘"
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '-'  ; '-'|'+';
+         _   ;    _   ;    _   |]
+  | [|   _   ;    _   ;    _   ;
+      '-'|'+';   '-'  ;    _   ;
+         _   ;    _   ;    _   |] -> u "─"
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '|'  ;    _   ;
+         _   ; '|'|'+';    _   |]
+  | [|   _   ; '|'|'+';    _   ;
+         _   ;   '|'  ;    _   ;
+         _   ;    _   ;    _   |] -> u "│"
+  | [|   _   ; '|'|'+';    _   ;
+        '='  ;   '+'  ;   '='  ;
+         _   ; '|'|'+';    _   |] -> u "╪"
+  | [|   _   ;    _   ;    _   ;
+        '='  ;   '+'  ;   '='  ;
+         _   ; '|'|'+';    _   |] -> u "╤"
+  | [|   _   ; '|'|'+';    _   ;
+        '='  ;   '+'  ;   '='  ;
+         _   ;    _   ;    _   |] -> u "╧"
+  | [|   _   ; '|'|'+';    _   ;
+         _   ;   '+'  ;   '='  ;
+         _   ; '|'|'+';    _   |] -> u "╞"
+  | [|   _   ; '|'|'+';    _   ;
+        '='  ;   '+'  ;    _   ;
+         _   ; '|'|'+';    _   |] -> u "╡"
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '+'  ;   '='  ;
+         _   ; '|'|'+';    _   |] -> u "╒"
+  | [|   _   ; '|'|'+';    _   ;
+         _   ;   '+'  ;   '='  ;
+         _   ;    _   ;    _   |] -> u "╘"
+  | [|   _   ;    _   ;    _   ;
+        '='  ;   '+'  ;    _   ;
+         _   ; '|'|'+';    _   |] -> u "╕"
+  | [|   _   ; '|'|'+';    _   ;
+        '='  ;   '+'  ;    _   ;
+         _   ;    _   ;    _   |] -> u "╛"
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '='  ; '='|'+';
+         _   ;    _   ;    _   |]
+  | [|   _   ;    _   ;    _   ;
+      '='|'+';   '='  ;    _   ;
+         _   ;    _   ;    _   |] -> u "═"
+  | [|   _   ;    _   ;    _   ;
+         _   ;    c   ;    _   ;
+         _   ;    _   ;    _   |] -> UChar.of_char c
+  | _ -> failwith "invalid pattern"
+
+let replace_pat_unsafe =
+  let both_double x y = if x = y then x else '-' in
+  let pick_double x s = UTF8.get s (if x = '=' then 1 else 0) in
+  function
+  | [| _ ; ' ';  _ ;
+      ' '; '+'; ' ';
+       _ ; ' ';  _ |] -> u "·"  (* XXX: maybe just leave the '+' *)
+  | [| _ ; ' ';  _ ;
+       _ ; '+'; ' ';
+       _ ; ' ';  _ |] -> u "╴"
+  | [| _ ; ' ';  _ ;
+      ' '; '+';  _ ;
+       _ ; ' ';  _ |] -> u "╶"
+  | [| _ ; ' ';  _ ;
+      ' '; '+'; ' ';
+       _ ;  _ ;  _ |] -> u "╷"
+  | [| _ ;  _ ;  _ ;
+      ' '; '+'; ' ';
+       _ ; ' ';  _ |] -> u "╵"
+  | [| _ ; ' ';  _ ;
+      ' '; '+';  x ;
+       _ ;  _ ;  _ |] -> pick_double x "┌╒"
+  | [| _ ;  _ ;  _ ;
+      ' '; '+';  x ;
+       _ ; ' ';  _ |] -> pick_double x "└╘"
+  | [| _ ; ' ';  _ ;
+       x ; '+'; ' ';
+       _ ;  _ ;  _ |] -> pick_double x "┐╕"
+  | [| _ ;  _ ;  _ ;
+       x ; '+'; ' ';
+       _ ; ' ';  _ |] -> pick_double x "┘╛"
+  | [| _ ; ' ';  _ ;
+       x ; '+';  y ;
+       _ ;  _ ;  _ |] -> pick_double (both_double x y) "┬╤"
+  | [| _ ;  _ ;  _ ;
+       x ; '+';  y ;
+       _ ; ' ';  _ |] -> pick_double (both_double x y) "┴╧"
+  | [| _ ;  _ ;  _ ;
+      ' '; '+';  x ;
+       _ ;  _ ;  _ |] -> pick_double x "├╞"
+  | [| _ ;  _ ;  _ ;
+       x ; '+'; ' ';
+       _ ;  _ ;  _ |] -> pick_double x "┤╡"
+  | [| _ ;  _ ;  _ ;
+       x ; '+';  y ;
+       _ ;  _ ;  _ |] -> pick_double (both_double x y) "┼╪"
+
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '-'  ; '-'|'+';
+         _   ;    _   ;    _   |]
+  | [|   _   ;    _   ;    _   ;
+      '-'|'+';   '-'  ;    _   ;
+         _   ;    _   ;    _   |] -> u "─"
+
+  | [|   _   ;    _   ;    _   ;
+         _   ;   '|'  ;    _   ;
+         _   ; '|'|'+';    _   |]
+  | [|   _   ; '|'|'+';    _   ;
+         _   ;   '|'  ;    _   ;
+         _   ;    _   ;    _   |] -> u "│"
+
+  | [|     _     ;  _ ; _ ;
+      '-'|'='|'+'; '='; _ ;
+           _     ;  _ ; _ |]
+  | [|_ ;  _ ;      _     ;
+      _ ; '='; '-'|'='|'+';
+      _ ;  _ ;      _     |] -> u "═"
+
+  | [|_; _; _;
+      _; c; _;
+      _; _; _|] -> UChar.of_char c
+
+  | _ -> failwith "invalid pattern"
+
+let () =
+  let args = List.tl (Array.to_list Sys.argv) in
+  let replace_pat =
+    if List.mem "-s" args
+      then replace_pat_safe
+      else replace_pat_unsafe
+  in
+  stdin |> input_all |> pad_right |> artify replace_pat |> print_endline

+ 13 - 0
graph.txt

@@ -0,0 +1,13 @@
+        a
+   +----+----+
+   b         c
+          +--+--------+
+          d  e  this is big F
+
+        a
+   +====+====+
+   b         c
+             |
+          +==+========+=== right
+          |  |        |
+          d  e  this is big F

+ 18 - 0
simple.txt

@@ -0,0 +1,18 @@
++---+
+|   |
++---+
+
+
++------+-----+
+|      |     |
++------+-----+
+
+++
+++
+
++--+
++--+
+
+++
+||
+++

+ 18 - 0
table.txt

@@ -0,0 +1,18 @@
++-------+----+
+| Head1 | H2 |
++===+===+====+
+| a | b | c  |
++---+---+----+
+
+
++-------+----+
+| Head1 | H2 |
++===+===+====+ +-------+----+
+| a | b | c  | | H1    | H2 |
++---+---+----+ +===+===+====+
+               | a | b | c  |
++-------+----+ +---+---+----+
+| Head1 | H2 |
++===+===+====+
+| a | b | c  |
++---+---+----+