/usr/share/gauche-0.9/site/lib/wiliki/format.scm is in wiliki 0.6.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | ;;;
;;; wiliki/format.scm - format wiki pages (backward compatibility module)
;;;
;;; Copyright (c) 2003-2009 Shiro Kawai <shiro@acm.org>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without restriction,
;;; including without limitation the rights to use, copy, modify,
;;; merge, publish, distribute, sublicense, and/or sell copies of
;;; the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
;;; AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
;;; OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
;;; IN THE SOFTWARE.
;;;
;;; $Id: format.scm,v 1.46 2007-12-22 00:05:06 shirok Exp $
(define-module wiliki.format
(use srfi-1)
(use srfi-2)
(use srfi-11)
(use srfi-13)
(use text.html-lite)
(use text.tree)
(use text.tr)
(use rfc.uri)
(use util.list)
(use util.queue)
(use util.match)
(use gauche.parameter)
(use gauche.charconv)
(use gauche.sequence)
(use wiliki.parse)
(use wiliki.page)
(use sxml.tools)
(export <wiliki-formatter-base>
wiliki:persistent-page?
wiliki:transient-page?
wiliki:format-wikiname
wiliki:format-macro
wiliki:format-time
wiliki:format-content
wiliki:formatter
wiliki:format-page-header
wiliki:format-page-content
wiliki:format-page-footer
wiliki:format-page-body
wiliki:format-head-elements
wiliki:format-head-title
wiliki:format-page
wiliki:format-line-plainly
wiliki:calculate-heading-id
wiliki:sxml->stree
wiliki:format-diff-pre
wiliki:format-diff-line
)
)
(select-module wiliki.format)
;; This module provides a base class <wiliki-formatter-base> as an anchor
;; point of customizing various formatting functions.
;;
;; The base class and methods only implements minimum functionalities, that
;; do not depend on persistent database. Subclass this class and specialize
;; method if you're writing a formatter for non-web targets (e.g. formatting
;; for a plain text).
;;
;; The <wiliki-formatter> class in wiliki.scm provides a full feature of
;; to generate HTML page. If you're customizing webpage formatting, use
;; that class as a starting point.
(define-class <wiliki-formatter-base> ()
(;; The following slots are only for compatibility to the code
;; written with WiLiKi-0.5_pre2.
;; They won't be supported officially in future versions; use
;; subclassing & methods instead.
(bracket :init-keyword :bracket
:init-value (lambda (name) (list #`"[[,|name|]]")))
(macro :init-keyword :macro
:init-value (lambda (expr context)
`("##" ,(write-to-string expr))))
(time :init-keyword :time
:init-value (lambda (time) (x->string time)))
(body :init-keyword :body
:init-value (lambda (page opts) (fmt-body page opts)))
(header :init-keyword :header
:init-value (lambda (page opts) '()))
(footer :init-keyword :footer
:init-value (lambda (page opts) '()))
(content :init-keyword :content
:init-value (lambda (page opts)
(wiliki:format-content page)))
(head-elements :init-keyword :head-elements
:init-value (lambda (page opts) '()))
))
;; Global context and the default formatter
(define the-formatter
(make-parameter (make <wiliki-formatter-base>)))
;; similar to sxml:sxml->xml, but deals with stree node, which
;; embeds a string tree.
(define (wiliki:sxml->stree sxml)
(define (sxml-node type body)
(define (attr lis r)
(cond ((null? lis) (reverse! r))
((not (= (length+ (car lis)) 2))
(error "bad attribute in node: " (cons type body)))
(else
(attr (cdr lis)
(cons `(" " ,(html-escape-string (x->string (caar lis)))
"=\"" ,(html-escape-string (x->string (cadar lis)))
"\"")
r)))))
(define (rest type lis)
(if (and (null? lis)
(memq type '(br area link img param hr input col base meta)))
'(" />")
(list* ">" (reverse! (fold node '() lis)) "</" type "\n>")))
(if (and (pair? body)
(pair? (car body))
(eq? (caar body) '@))
(list* "<" type (attr (cdar body) '()) (rest type (cdr body)))
(list* "<" type (rest type body)))
)
(define (node n r)
(cond
((string? n) (cons (html-escape-string n) r))
((and (pair? n) (symbol? (car n)))
(if (eq? (car n) 'stree)
(cons (cdr n) r)
(cons (sxml-node (car n) (cdr n)) r)))
(else
;; badly formed node. we show it for debugging ease.
(cons (list "<span class=\"wiliki-alert\">"
(html-escape-string (format "~,,,,50:s" n))
"</span\n>")
r))))
(node sxml '()))
;;=================================================
;; Formatting: Wiki -> SXML
;;
;; Utility to generate a (mostly) unique id for the headings.
;; Passes a list of heading string stack.
(define (wiliki:calculate-heading-id headings)
(string-append "H-" (number->string (hash headings) 36)))
;; Backward compatibility
(define wiliki:format-line-plainly wiliki-remove-markup)
;; Page ======================================================
(define (wiliki:format-content page)
(define (do-fmt content)
(expand-page (wiliki-parse-string content)))
(cond ((string? page) (do-fmt page))
((is-a? page <wiliki-page>)
(if (wiliki-page-circular? page)
;; loop in $$include chain detected
`(p ">>>$$include loop detected<<<")
(parameterize
((wiliki-page-stack (cons page (wiliki-page-stack))))
(if (string? (ref page 'content))
(let1 sxml (do-fmt (ref page 'content))
(set! (ref page'content) sxml)
sxml)
(ref page 'content)))))
(else page)))
;; [SXML] -> [SXML], expanding wiki-name and wiki-macro nodes.
;;
(define (expand-page sxmls)
(let rec ((sxmls sxmls)
(hctx '())) ;;headings context
(match sxmls
(() '())
((('wiki-name name) . rest)
(append (wiliki:format-wikiname (the-formatter) name)
(rec rest hctx)))
((('wiki-macro . expr) . rest)
(append (wiliki:format-macro (the-formatter) expr 'inline)
(rec rest hctx)))
(((and ((or 'h2 'h3 'h4 'h5 'h6) . _) sxml) . rest)
;; extract heading hierarchy to calculate heading id
(let* ((hn (sxml:name sxml))
(hkey (assq 'hkey (sxml:aux-list-u sxml)))
(hctx2 (extend-headings-context hctx hn hkey)))
(cons `(,hn ,@(if hkey
`((@ (id ,(heading-id hctx2))))
'())
,@(rec (sxml:content sxml) hctx))
(rec rest hctx2))))
(((and (name . _) sxml) . rest);; generic node
(cons `(,name ,@(cond ((sxml:attr-list-node sxml) => list)
(else '()))
,@(rec (sxml:content sxml) hctx))
(rec rest hctx)))
((other . rest)
(cons other (rec rest hctx))))))
(define (hn->level hn)
(find-index (cut eq? hn <>) '(h2 h3 h4 h5 h6)))
(define (extend-headings-context hctx hn hkey)
(if (not hkey)
hctx
(let* ((level (hn->level hn))
(up (drop-while (lambda (x) (>= (hn->level (car x)) level)) hctx)))
(acons hn (cadr hkey) up))))
(define (heading-id hctx)
(wiliki:calculate-heading-id (map cdr hctx)))
;; default page body formatter
(define (fmt-body page opts)
`(,@(wiliki:format-page-header page opts)
,@(wiliki:format-page-content page opts)
,@(wiliki:format-page-footer page opts)))
;;;
;;; Exported functions
;;;
(define wiliki:formatter the-formatter)
;; Default formatting methods.
;; Methods are supposed to return SXML nodeset.
;; NB: It is _temporary_ that these methods calling the slot value
;; of the formatter, just to keep the backward compatibility to 0.5_pre2.
;; Do not count on this implementation. The next release will remove
;; all the closure slots of <wiliki-formatter-base> and the default behavior
;; will directly be embedded in these methods.
(define-method wiliki:format-wikiname ((fmt <wiliki-formatter-base>) name)
((ref fmt 'bracket) name))
(define-method wiliki:format-wikiname ((name <string>))
(wiliki:format-wikiname (the-formatter) name))
(define-method wiliki:format-macro ((fmt <wiliki-formatter-base>) expr context)
((ref fmt 'macro) expr context))
(define-method wiliki:format-macro (expr context)
(wiliki:format-macro (the-formatter) expr context))
(define-method wiliki:format-time ((fmt <wiliki-formatter-base>) time)
((ref fmt 'time) time))
(define-method wiliki:format-time (time)
(wiliki:format-time (the-formatter) time))
(define-method wiliki:format-page-content ((fmt <wiliki-formatter-base>)
page ;; may be a string
. options)
((ref fmt 'content) page options))
(define-method wiliki:format-page-content (page . opts)
(apply wiliki:format-page-content (the-formatter) page opts))
(define-method wiliki:format-page-body ((fmt <wiliki-formatter-base>)
(page <wiliki-page>)
. opts)
`(,@(apply wiliki:format-page-header page opts)
,@(apply wiliki:format-page-content page opts)
,@(apply wiliki:format-page-footer page opts)))
(define-method wiliki:format-page-body ((page <wiliki-page>) . opts)
(apply wiliki:format-page-body (the-formatter) page opts))
(define-method wiliki:format-page-header ((fmt <wiliki-formatter-base>)
(page <wiliki-page>)
. options)
((ref fmt 'header) page options))
(define-method wiliki:format-page-header ((page <wiliki-page>) . opts)
(apply wiliki:format-page-header (the-formatter) page opts))
(define-method wiliki:format-page-footer ((fmt <wiliki-formatter-base>)
(page <wiliki-page>)
. options)
((ref fmt 'footer) page options))
(define-method wiliki:format-page-footer ((page <wiliki-page>) . opts)
(apply wiliki:format-page-footer (the-formatter) page opts))
(define-method wiliki:format-head-elements ((fmt <wiliki-formatter-base>)
(page <wiliki-page>)
. options)
(append
((ref fmt 'head-elements) page options)
(ref page 'extra-head-elements)))
(define-method wiliki:format-head-elements ((page <wiliki-page>) . opts)
(apply wiliki:format-head-elements (the-formatter) page opts))
(define-method wiliki:format-head-title ((fmt <wiliki-formatter-base>)
(page <wiliki-page>) . options)
(ref page'title))
(define-method wiliki:format-page ((fmt <wiliki-formatter-base>)
(page <wiliki-page>)
. opts)
`(html
(head ,@(apply wiliki:format-head-elements fmt page opts))
(body ,@(apply wiliki:format-page-body fmt page opts))))
(define-method wiliki:format-page ((page <wiliki-page>) . opts)
(apply wiliki:format-page (the-formatter) page opts))
(define (wiliki:persistent-page? page)
(not (wiliki:transient-page? page)))
(define (wiliki:transient-page? page)
(not (ref page 'key)))
;; NB: these should also be a generics.
(define (wiliki:format-diff-pre difflines)
`(pre (@ (class "diff")
(style "background-color:#ffffff; color:#000000; margin:0"))
,@(map wiliki:format-diff-line difflines)))
(define (wiliki:format-diff-line line)
(define (aline . c)
`(span (@ (class "diff_added")
(style "background-color:#ffffff; color: #4444ff"))
,@c))
(define (dline . c)
`(span (@ (class "diff_deleted")
(style "background-color:#ffffff; color: #ff4444"))
,@c))
(cond ((string? line) `(span " " ,line "\n"))
((eq? (car line) '+) (aline "+ " (cdr line) "\n"))
((eq? (car line) '-) (dline "- " (cdr line) "\n"))
(else "???")))
(provide "wiliki/format")
|