Commits

Paweł Wieczorek committed c56ac47 Draft

initial

Comments (0)

Files changed (10)

+Pawel Wieczorek <wieczyk gmail>
+OCAMLBUILD= ocamlbuild -use-ocamlfind
+OCAMLFIND = ocamlfind
+
+SOURCE_DIRS=\
+	-Is src\
+
+DEMO_DIRS=\
+	-Is demo\
+
+LIBS_=src/ManpageRenderer.cma src/ManpageRenderer.cmxa
+
+LIBS =_build/src/ManpageRenderer.cma _build/src/ManpageRenderer.cmxa
+
+CMI=\
+	_build/src/ManpageRenderer.cmi
+
+LIB_FILES=\
+	${CMI}\
+	${LIBS}\
+	_build/src/ManpageRenderer.a
+
+.PHONY: ${LIBS} lib demo
+
+all: lib
+
+install: lib
+	${OCAMLFIND} install ManpageRenderer META ${LIB_FILES}
+
+remove deinstall uninstall:
+	${OCAMLFIND} remove ManpageRenderer
+
+doc:
+	${OCAMLBUILD} ${SOURCE_DIRS} src/ManpageRenderer.docdir/index.html
+
+lib:
+	${OCAMLBUILD} ${SOURCE_DIRS} ${LIBS_}
+
+demo: lib
+	cd demo; ${MAKE} all
+
+clean:
+	${OCAMLBUILD} ${SOURCE_DIRS} ${LIBS_} -clean
+	cd demo; ${MAKE} clean
+
+<src/**.cmx>: for-pack(ManpageRenderer)
+<demo/*>: package(manpage-renderer)
+open ManpageRenderer.Document
+open ManpageRenderer.Renderer
+open ManpageRenderer.Viewer
+
+
+let rec demo_document = Document 
+    ( "manpage-renderer"
+    , SUBROUTINES
+    , "Marz 19, 2003"
+    , "version 1.0"
+    , "Manpage Renderer manual"
+    , demo_sections
+    )
+
+and demo_sections =
+    [ demo_name_section
+    ; demo_synopsis_section
+    ; demo_description_section
+    ]
+
+and demo_name_section = Section ("NAME", 
+    [ Paragraph
+        [ Normal [ "manpage-renderer" ]
+        ]
+    ])
+
+and demo_synopsis_section = Section ("MODULES", 
+    [ Paragraph
+        [ Normal [ "ManpageRenderer.Document" ]
+        ]
+    ; Paragraph
+        [ Normal [ "ManpageRenderer.Renderer" ]
+        ]
+    ; Paragraph
+        [ Normal [ "ManpageRenderer.Viewer" ]
+        ]
+    ])
+
+and demo_description_section = Section ("DESCRIPTION", 
+    [ Paragraph
+        [ Normal [ "cdspeed" ]
+        ]
+    ])
+
+;;
+
+
+
+run_man_command demo_document
+
+;;
+
+print_endline (render_document demo_document)
+OCAMLC=ocamlc
+
+DEMO=ManpageRenderer.byte
+
+.PHONY : ${DEMO}
+
+all: ${DEMO}
+
+${DEMO}: Demo.ml
+	${OCAMLC} unix.cma -I ../_build/src -o ${DEMO} ManpageRenderer.cma Demo.ml
+
+clean:
+	rm -f ${DEMO} Demo.cmo Demo.cmi 
+
+
+
+type document
+    = Document of string * manpage_section * string * string * string * section list
+
+and manpage_section
+    = COMMAND
+    | SYSTEM_CALLS
+    | SUBROUTINES
+    | DEVICES
+    | FILE_FORMATS
+    | GAMES
+    | MISC
+    | SYSTEM_ADMINISTRATION
+    | ANOTHER
+    | NEW_DOCUMENTATION
+    | LOCAL_DOCUMENTATION
+
+
+and section
+    = Section of string * content list
+
+and content
+    = Paragraph of word list
+
+and word
+    = Normal of string list
+    | Emph of string list
+
+let rec demo_document = Document 
+    ( "cdspeed"
+    , COMMAND
+    , "September 10, 2003"
+    , "version 0.3"
+    , "USER COMMANDS"
+    , demo_sections
+    )
+
+and demo_sections =
+    [ demo_name_section
+    ; demo_synopsis_section
+    ; demo_description_section
+    ]
+
+and demo_name_section = Section ("NAME", 
+    [ Paragraph
+        [ Normal [ "cdspeed - change speed of CD-ROM" ]
+        ]
+    ])
+
+and demo_synopsis_section = Section ("SYNOPSIS", 
+    [ Paragraph
+        [ Normal [ "cdspeed [-h]" ]
+        ]
+    ])
+
+and demo_description_section = Section ("DESCRIPTION", 
+    [ Paragraph
+        [ Normal [ "cdspeed" ]
+        ]
+    ])
+
+let add_strings_to_buffer buffer xs =
+    List.iter (Buffer.add_string buffer) xs
+
+let add_words_to_buffer buffer xs =
+    List.iter (fun word -> Buffer.add_string buffer word; Buffer.add_string buffer " ") xs
+
+let add_strings_to_buffer_ln buffer xs =
+    add_strings_to_buffer buffer xs;
+    Buffer.add_char buffer '\n'
+
+let add_words_to_buffer_ln buffer xs =
+    add_words_to_buffer buffer xs;
+    Buffer.add_char buffer '\n'
+
+let tag_of_manpage_section = function
+    | COMMAND -> "1"
+    | SYSTEM_CALLS -> "2"
+    | SUBROUTINES -> "3"
+    | DEVICES -> "4"
+    | FILE_FORMATS -> "5"
+    | GAMES -> "6"
+    | MISC -> "7"
+    | SYSTEM_ADMINISTRATION -> "8"
+    | ANOTHER -> "9"
+    | NEW_DOCUMENTATION -> "n"
+    | LOCAL_DOCUMENTATION -> "l"
+
+
+let compile_word buffer = function
+    | Normal texts ->
+        add_words_to_buffer_ln buffer texts
+
+let compile_content buffer = function
+    | Paragraph words ->
+        List.iter (compile_word buffer) words
+
+let compile_section buffer = function
+    | Section (title, contents) ->
+        add_strings_to_buffer_ln buffer
+            [ ".SH "
+            ; title
+            ];
+        add_strings_to_buffer_ln buffer
+            [ ".TP"
+            ];
+        List.iter (compile_content buffer) contents;
+        Buffer.add_string buffer "\n"
+
+let compile_document buffer = function
+    | Document (name, manpage_section, center_footer, left_footer, center_header, sections) ->
+    add_strings_to_buffer_ln buffer
+        [ ".TH "
+        ; name
+        ; " "
+        ; tag_of_manpage_section manpage_section
+        ; " \""
+        ; center_footer
+        ; "\" \""
+        ; left_footer
+        ; "\" \""
+        ; center_header
+        ; "\" "
+        ];
+    List.iter (compile_section buffer) sections;
+
+    Buffer.contents buffer
+    
+
+let render_document document =
+    let buffer = Buffer.create 4096 in
+    compile_document buffer document
+
+
+
+let output = render_document demo_document
+
+
+let run_man_command_for_output output =
+    let out_channel = Unix.open_process_out "man -l -" in
+    output_string out_channel output;
+    Unix.close_process_out out_channel
+
+let run_man_command document =
+    let output = render_document document in
+    run_man_command_for_output output
+
+;;
+
+run_man_command demo_document
+;;
+
+print_endline output
+
+
+
+type document
+    = Document of string * manpage_section * string * string * string * section list
+
+and manpage_section
+    = COMMAND
+    | SYSTEM_CALLS
+    | SUBROUTINES
+    | DEVICES
+    | FILE_FORMATS
+    | GAMES
+    | MISC
+    | SYSTEM_ADMINISTRATION
+    | ANOTHER
+    | NEW_DOCUMENTATION
+    | LOCAL_DOCUMENTATION
+
+
+and section
+    = Section of string * content list
+
+and content
+    = Paragraph of word list
+
+and word
+    = Normal of string list
+    | Emph of string list
+

src/ManpageRenderer.mlpack

+Document
+Renderer
+Viewer
+open Document
+
+let add_strings_to_buffer buffer xs =
+    List.iter (Buffer.add_string buffer) xs
+
+let add_words_to_buffer buffer xs =
+    List.iter (fun word -> Buffer.add_string buffer word; Buffer.add_string buffer " ") xs
+
+let add_strings_to_buffer_ln buffer xs =
+    add_strings_to_buffer buffer xs;
+    Buffer.add_char buffer '\n'
+
+let add_words_to_buffer_ln buffer xs =
+    add_words_to_buffer buffer xs;
+    Buffer.add_char buffer '\n'
+
+let tag_of_manpage_section = function
+    | COMMAND -> "1"
+    | SYSTEM_CALLS -> "2"
+    | SUBROUTINES -> "3"
+    | DEVICES -> "4"
+    | FILE_FORMATS -> "5"
+    | GAMES -> "6"
+    | MISC -> "7"
+    | SYSTEM_ADMINISTRATION -> "8"
+    | ANOTHER -> "9"
+    | NEW_DOCUMENTATION -> "n"
+    | LOCAL_DOCUMENTATION -> "l"
+
+
+let compile_word buffer = function
+    | Normal texts ->
+        add_words_to_buffer_ln buffer texts
+
+let compile_content buffer = function
+    | Paragraph words ->
+        add_strings_to_buffer_ln buffer
+            [ ".P"
+            ];
+        List.iter (compile_word buffer) words
+
+let compile_section buffer = function
+    | Section (title, contents) ->
+        add_strings_to_buffer_ln buffer
+            [ ".SH "
+            ; title
+            ];
+        add_strings_to_buffer_ln buffer
+            [ ".TP"
+            ];
+        List.iter (compile_content buffer) contents;
+        Buffer.add_string buffer "\n"
+
+let compile_document buffer = function
+    | Document (name, manpage_section, center_footer, left_footer, center_header, sections) ->
+    add_strings_to_buffer_ln buffer
+        [ ".TH "
+        ; name
+        ; " "
+        ; tag_of_manpage_section manpage_section
+        ; " \""
+        ; center_footer
+        ; "\" \""
+        ; left_footer
+        ; "\" \""
+        ; center_header
+        ; "\" "
+        ];
+    List.iter (compile_section buffer) sections;
+
+    Buffer.contents buffer
+    
+
+let render_document document =
+    let buffer = Buffer.create 4096 in
+    compile_document buffer document
+
+
+
+open Renderer
+
+let run_man_command_for_output output =
+    let out_channel = Unix.open_process_out "man -l -" in
+    output_string out_channel output;
+    Unix.close_process_out out_channel
+
+let run_man_command document =
+    let output = render_document document in
+    run_man_command_for_output output
+