colorize icon indicating copy to clipboard operation
colorize copied to clipboard

why the result doesn't contains <br> ?

Open muyinliu opened this issue 10 years ago • 6 comments

After colorize, the result HTML doesn't contains <br> at all. Why? The source code file did contains \n.

muyinliu avatar Jul 04 '14 01:07 muyinliu

A hack for colorize:colorize-file newlines and spaces (in vim regex syntax). An offset is required not to hit the style header.

; replace newlines with <br> and a newline
s/\n/<br>\r/g
; replace spaces between tags with nbsp
s/>\zs\ \+\ze</\=substitute(submatch(0), ' ', '\&nbsp;', 'g')/g
; replace spaces between the <br>\n and another tag
s/>\n\zs\ \+\ze</\=substitute(submatch(0), ' ', '\&nbsp;', 'g')/g

tgbugs avatar Apr 06 '18 21:04 tgbugs

I also have this problem - expected the colorized code to be formatted the same as original, but in HTML everything is compacted into a single line.

avodonosov avatar Oct 22 '22 16:10 avodonosov

After running colorize:colorize-file I opened the generated file and wrapped the code into <pre> ... </pre> - looks good.

avodonosov avatar Oct 22 '22 16:10 avodonosov

@muyinliu, it is better to change the issue title to "why newlines disappear?" or "why colorized code is compacted into a single line?". The <br> element was not the intended, the library was actually designed to wrap the code into <pre>, so the current issue title is misleading somewhat.

avodonosov avatar Oct 22 '22 19:10 avodonosov

The problem is caused by mistake in the colorize-file-to-stream implementation:

Despite the default value for the encoder parameter is 'encode-for-pre https://github.com/kingcons/colorize/blob/ea676b584e0899cec82f21a9e6871172fe3c0eb5/colorize.lisp#L299 the function wraps the colorized code into <tt>: https://github.com/kingcons/colorize/blob/ea676b584e0899cec82f21a9e6871172fe3c0eb5/colorize.lisp#L324

Note also the docstring saying " To wrap in a <tt> element rather than <pre>, pass 'encode-for-tt as the ENCODER." which confirms that with the default encoder the intention was to wrap the colorized code into <pre>.

avodonosov avatar Oct 22 '22 19:10 avodonosov

Workaround for the current version of the library: explicitly pass 'encode-for-tt which is consistent with the hardcoded wrapper tag <tt>...</tt>.

For that, instead of


(colorize:colorize-file :common-lisp
                        (asdf:system-relative-pathname :cl+ssl
                                                       "src/package.lisp"))

do

(let* ((input-file (asdf:system-relative-pathname :cl+ssl
                                                  "src/package.lisp"))
       (output-file (make-pathname :type "html"
                                   :defaults input-file)))
  (with-open-file (stream output-file :direction :output :if-exists :supersede)
    (colorize:colorize-file-to-stream :common-lisp
                                      input-file
                                      stream
                                      :encoder 'encode-for-tt)))

The proposed pull request #13 fixes implementation to chose the wrapper tag according to the encoder function, thus fixing colorize:colorize-file which passes 'encode-for-pre.

avodonosov avatar Oct 22 '22 20:10 avodonosov