Text to HTML: Difference between revisions

1,522 bytes added ,  9 years ago
→‎{{header|Racket}}: New Racket solution
(→‎Tcl: Added implementation)
(→‎{{header|Racket}}: New Racket solution)
Line 150:
return root;
}</lang>
 
=={{header|Racket}}==
 
This task seems like it's very under-defined, but the discussion seems to be headed towards a simple markdown specification. I therefore do this with a small interface to [https://github.com/jgm/cmark cmark] to render [http://commonmark.org/ commonmark] text.
 
(Note that this is not some cooked code, it's coming from code that I'm using to render class notes, and hopefully it will be useful to have such an example here. It cetrainly seems to me as a useful thing compared to some half-baked not-really-markdown-or-anything implementation.)
 
<lang racket>
#lang at-exp racket
 
(require ffi/unsafe ffi/unsafe/define)
 
(define-ffi-definer defcmark (ffi-lib "libcmark"))
 
(define _cmark_opts
(_bitmask '(sourcepos = 1 hardbreaks = 2 normalize = 4 smart = 8)))
(define-cpointer-type _node)
(defcmark cmark_markdown_to_html
(_fun [bs : _bytes] [_int = (bytes-length bs)] _cmark_opts
-> [r : _bytes] -> (begin0 (bytes->string/utf-8 r) (free r))))
 
(define (cmark-markdown-to-html #:options [opts '(normalize smart)] . text)
(cmark_markdown_to_html (string->bytes/utf-8 (string-append* text)) opts))
 
(display @cmark-markdown-to-html{
This is
a paragraph
 
a block of
code
 
* A one-bullet list
> With quoted text
>
> and code
})
</lang>
 
{{out}}
<pre>
<p>This is
a paragraph</p>
<pre><code>a block of
code
</code></pre>
<ul>
<li>A one-bullet list
<blockquote>
<p>With quoted text</p>
<pre><code>and code
</code></pre>
</blockquote>
</li>
</ul>
</pre>
 
=={{header|Tcl}}==