Browse Source

mad man groff

Namdak Tonpa 5 years ago
parent
commit
c246d1cc09
4 changed files with 31 additions and 19 deletions
  1. 1 1
      include/mad.hrl
  2. BIN
      mad
  3. 16 14
      src/mad_groff.erl
  4. 14 4
      src/mad_man.erl

+ 1 - 1
include/mad.hrl

@@ -1 +1 @@
--define(VERSION,"5.4-24-g10c5ef5").
+-define(VERSION,"5.4-25-geca70a1").

BIN
mad


+ 16 - 14
src/mad_groff.erl

@@ -4,10 +4,12 @@
 
 do(FileName) ->
   {#xmlElement{content=[#xmlElement{content=Head}, BodyTree | _]}, _} = xmerl_scan:file(FileName),
-  FN = hd( string:split(FileName, ".", leading) ),
-  write2new(FN, lists:reverse( show(BodyTree, false, false,
-    [ [".TH ", FN, " 1 \"n2o 4.5.0\" \"Synrc Research Center\" \"", head(Head, ""), "\"", "\n",
-       ".SH NAME", "\n", "UNKNOWN", "\n"] ]) ) ), ok.
+  FN = string:join(lists:reverse(tl(lists:reverse(string:tokens(FileName,".")))),"."),
+  Groff = "groff/"++FN,
+  filelib:ensure_dir(filename:dirname(Groff)++"/"),
+  write2new(Groff, lists:reverse( show(BodyTree, false, false,
+    [ [".TH ", FN, " 1 \"",FN,"\" \"Synrc Research Center\" \"", head(Head, ""), "\"", "\n",
+       ".SH NAME", "\n", FN, "\n"] ]))), ok.
 
 show(#xmlElement{name=section,content=C},false,_,RA) -> S = check(C, false), child(C,S,[also(S)|RA]);
 show(#xmlElement{content=C},false,_,RA) -> child(C,false,RA);
@@ -15,9 +17,9 @@ show(#xmlElement{name=h3,content=C},{true,S2},_,RA) -> child(C,{true,S2},[".SH "
 show(#xmlElement{name=a,content=[#xmlText{value=V}|_]},{true, last},_,RA) ->
   [["\\fB\\fI", V,"(1)","\\fR\\&\\fR\\&",", "]|RA];
 show(#xmlElement{name=a,content=[#xmlText{value=V}|CM]},S,false,RA) ->
-  child(CM, S, [["\\fI", V, "\\fR\\& "]|RA]);
+  child(CM, S, [["\\fI",V,"\\fR\\& "]|RA]);
 show(#xmlElement{name=a,content=[#xmlText{value=V}|CM]},S,true,RA) ->
-  child(CM, S, [["\\fI", V, "\\fR\\&"]|RA]);
+  child(CM, S, [["\\fI",V,"\\fR\\&"]|RA]);
 show(#xmlElement{name=code,content=[#xmlText{value=V} | CM]},{true,S2},_,RA) ->
   child(CM, {true, S2}, [[".nf","\n",ltrim(V),".fi","\n"]|RA]);
 show(#xmlElement{name=p,content=C}, {true, S2}, _, RA) -> child(C,{true,S2}, ["\n",".LP"|RA]);
@@ -26,14 +28,14 @@ show(#xmlElement{name=_, content=C}, S, _, RA) -> child(C,S,RA);
 show(_,false,_,RA) -> RA;
 show(_,{true,last},_,RA) -> RA;
 show(#xmlText{value=V},_S,_,RA) ->
-  G = (hd(V) == 10) andalso (string:trim(tl(V)) == ""),
+  G = (hd(V) == 10) andalso (mad_man:trim(tl(V)) == ""),
   if G -> RA; true ->  [ltrim(V) | RA] end;
 show(_,_,_,RA) -> RA.
 
 check([#xmlElement{name=h3,content=C}|_], false) -> check(C, {true, h3});
 check([#xmlElement{name=p,content=C}|_], false) -> check(C, {true, p});
 check([#xmlText{value=V}|MoreNodes], false) ->
-  G = (hd(V) == 10) andalso (string:trim(tl(V)) == ""),
+  G = (hd(V) == 10) andalso (mad_man:trim(tl(V)) == ""),
   if G -> check(MoreNodes, false); true -> {true, other} end;
 check([], {true, h3}) -> {true, other};
 check([#xmlText{value=_V} | _], {true, h3}) -> {true, usual};
@@ -54,16 +56,16 @@ child([Node | MoreNodes], S, RA) ->
 pt({true,last},_,_) -> false;
 pt(_,_,[]) -> false;
 pt(_,#xmlElement{name=a},[#xmlText{value=V}|_])->
-  V2=string:trim(V),if V2 =/= "",hd(V2)==46->true;true->false end;
+  V2=mad_man:trim(V),if V2 =/= "",hd(V2)==46->true;true->false end;
 pt(_,_,_) -> false.
 
 also({true, last}) -> ["\n", ".SH ALSO", "\n"];
 also(_) -> "".
 
 ltrim(V) ->
-  [ [ string:trim(V2, both), "\n"]
-   || V2 <- string:split(V, "\n", all),
-      string:trim(V2, both) =/= ""].
+  [ [ mad_man:trim(V2), "\n"]
+   || V2 <- string:tokens(V, "\n"),
+      mad_man:trim(V2) =/= ""].
 
 head([], A) -> A;
 head([#xmlElement{name=title, content=[#xmlText{value=V} | _]} | _], _) -> V;
@@ -72,5 +74,5 @@ head([_|T], A) -> head(T, A).
 write2new(F, S) ->
   file:write_file(F ++ ".1",
   io_lib:fwrite("~s", [
-  string:trim(
-  unicode:characters_to_binary(S,utf8),trailing,", ")]),[append]).
+  mad_man:trim(
+  unicode:characters_to_binary(S,utf8))]), [append]).

+ 14 - 4
src/mad_man.erl

@@ -9,18 +9,28 @@ man(["html"]) ->
          ++ filelib:wildcard("src/**/*.erl") ],
    {ok,man};
 
+man(["groff"]) ->
+   case lists:all(fun(X) -> mad_groff:do(X) == ok end,
+        filelib:wildcard("man/**/*.htm")
+     ++ filelib:wildcard("articles/**/*.htm")
+     ++ filelib:wildcard("*.html")
+     ++ filelib:wildcard("*.htm")) of
+        true -> {ok,check};
+        false -> {error,check} end;
+
 man(["check"]) ->
    case lists:all(fun(X) -> element(1,X) == ok end, [ check(I)
-    || I <- filelib:wildcard("man/**/*.htm")
-         ++ filelib:wildcard("articles/**/*.htm")
+    || I <- filelib:wildcard("*.htm")
          ++ filelib:wildcard("*.html")
-         ++ filelib:wildcard("*.htm") ]) of
+         ++ filelib:wildcard("man/**/*.htm")
+         ++ filelib:wildcard("articles/**/*.htm") ]) of
         true -> {ok,check};
         false -> {error,check} end.
 
 write(Gen,Bin) -> io:format("Generated: ~p~n",[Gen]), file:write_file(Gen,Bin).
 replace(S,A,B) -> re:replace(S,A,B,[global,{return,list}]).
-trim(A) -> re:replace(A, "(^\\s+)|(\\s+$)", "", [global,{return,list}]).
+trim(A) when is_list(A) -> trim(unicode:characters_to_binary(A));
+trim(A) when is_binary(A) -> re:replace(A, "(^\\s+)|(\\s+$)", "", [global,{return,list}]).
 fix([Prefix]) -> Prefix;
 fix([_Prefix,Name|_Rest]) -> Name.
 check(Filename) ->