root/lang/gauche/oldtype/trunk/src/oldtype_to @ 22567

Revision 22567, 5.9 kB (checked in by kiyoka, 5 years ago)

Supported 'Related pages:' feature.

  • Property svn:executable set to *
Line 
1#!/usr/local/bin/gosh
2;; -*- coding: utf-8 -*-
3;;;
4;;; oldtype_to - oldtype format to variouse document converter.
5;;;
6;;;  Copyright (c) 2007 Kiyoka Nishiyama, All rights reserved.
7;;;
8;;;  Permission is hereby granted, free of charge, to any person
9;;;  obtaining a copy of this software and associated documentation
10;;;  files (the "Software"), to deal in the Software without restriction,
11;;;  including without limitation the rights to use, copy, modify,
12;;;  merge, publish, distribute, sublicense, and/or sell copies of
13;;;  the Software, and to permit persons to whom the Software is
14;;;  furnished to do so, subject to the following conditions:
15;;;
16;;;  The above copyright notice and this permission notice shall be
17;;;  included in all copies or substantial portions of the Software.
18;;;
19;;;  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20;;;  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21;;;  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22;;;  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23;;;  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24;;;  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
25;;;  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26;;;  IN THE SOFTWARE.
27;;;
28;;; $Id: oldtype_to 198 2008-01-13 10:10:22Z kiyoka $
29;;;
30(use srfi-1)
31(use sxml.tools)
32(use sxml.serializer)
33(use text.html-lite)
34(use text.tree)
35(use gauche.parseopt)
36(use gauche.charconv)
37(use oldtype.parse)
38(use oldtype.format)
39(use oldtype.util)
40(use oldtype.page)
41(use oldtype.timeline)
42
43
44(define (oldtype:sxml->html sxml ot-path)
45  (let (
46        (html-body (srl:sxml->html sxml))
47        (wikiname  (oldtype:otpath->wikiname ot-path)))
48    (tree->string
49     `(
50       ,(html:html                                                     
51         (html:head
52          (html:meta :http-equiv "Content-Type"        :content "text/html; charset=utf-8")
53          (html:meta :http-equiv "Content-Style-Type"  :content "text/css")
54          (html:link :rel "stylesheet" :href "../design/oldtype.css" :type "text/css")
55          (html:title wikiname)
56          (html:body
57           (html:h1 wikiname)
58           html-body
59           (html:div :class "footer"
60                     "Generated by "
61                     (html:a :href oldtype:site-url
62                             "OldType")
63                     " version "
64                     oldtype:version)
65           )))))))
66
67
68;; Main -------------------------------------------------------
69(define (main args)
70
71  (define docstrings
72    `(
73      ,(string-append "oldtype_to version " oldtype:version)
74      ""
75      " Usage:"
76      "     oldtype_to [type] wikiname.ot [wikiname.log] [wikiname.ann] > wikiname.(html/sxml)"
77      ""
78      " type:"
79      "     internal, html, sxml, plain"
80      "     `internal` is a internal format for Kahua oldtype application."
81      " wikiname.ot:"
82      "     input file in OldType wiki format document."
83      " wikiname.log:"
84      "     result of 'svn log wikiname.ot' command."
85      " wikiname.ann:"
86      "     result of 'svn ann wikiname.ot' command."
87      ""
88      " Options:"
89      "     -h ... Display this help message."
90      ))
91
92  (define (cerr str)
93    (display str (standard-error-port))
94    (newline (standard-error-port)))
95
96  (define (display-help)
97    (for-each
98     (lambda (docstring) (cerr docstring))
99     docstrings)
100    (exit 4))
101
102  (define (parse-log-and-annotate rest-arg)
103    (case (length rest-arg)
104      ((0)
105       #f)
106      ((2)
107       ;; process [wikiname.log] and [wikiname.ann] file
108       (values
109        (oldtype:parse-log      (car rest-arg))
110        (oldtype:parse-annotate (cadr rest-arg))))
111      (else
112       (print "Error: You must specify both [wikiname.log] and [wikiname.ann] always.")
113       (exit 1))))
114 
115  (let* (
116         (wiki-mode #f)
117         ;; Dropping the first argument.
118         (_ (cdr args))
119         
120         ;; Checking switches.
121         (_ (parse-options
122             _
123             (
124              ("h" () (display-help)))))
125
126         (input-file (if (> 1 (length _))
127                         (display-help)
128                         (cadr _)))
129         (wikiname (regexp-replace #/.ot$/
130                                   (sys-basename input-file)
131                                   ""))
132         (type (string->symbol (car _)))
133         (rest (cddr _))
134
135         (converted-str
136          (port->string
137           (open-input-conversion-port
138            (open-input-file input-file)
139            "*JP")))
140
141         ;; Making string port from stdin/file
142         ;;
143         (input-port
144          (open-input-string converted-str)))
145    (case type
146      ('internal
147       (if (< 1 (length rest))
148           (let1 oldtype-page   (parse (make <oldtype-page> :name wikiname) input-port (car rest) (cadr rest))
149                 (pretty-print-sexp (serialize oldtype-page)))
150           (let1 oldtype-page   (parse (make <oldtype-page> :name wikiname) input-port #f #f)
151                 (pretty-print-sexp (serialize oldtype-page)))))
152      ('sxml
153       (let1 sxml (oldtype-parse input-port)
154             (pretty-print-sexp sxml)))
155      ('html
156       (let* (
157              (sxml           (oldtype-parse input-port))
158              (expanded-sxml  (oldtype:expand-page sxml #f #f)))
159         (display   (oldtype:sxml->html expanded-sxml input-file))))
160      ('plain
161       (let* (
162              (sxml           (oldtype-parse input-port))
163              (sexp           (oldtype:sxml->plain-text sxml #f)))
164         (print
165          (tree->string
166           sexp))))
167      ('rich
168       (let* (
169              (sxml           (oldtype-parse input-port))
170              (sexp           (oldtype:sxml->plain-text sxml #t)))
171         (print
172          (tree->string
173           sexp))))
174      ('commands
175       (let* (
176              (sxml           (oldtype-parse input-port))
177              (sexp           (oldtype:sxml->command-list sxml wikiname)))
178         (pretty-print-sexp sexp)))
179      (else
180       (cerr (format "Unsupported type. [~a]" (symbol->string type)))
181       (exit 1))))
182  (exit 0))
Note: See TracBrowser for help on using the browser.