On Sunday, May 28, 2017 at 10:20:10 PM UTC+2, Matthias Felleisen wrote:
> I have the impression that you are conflating rendering as an output with
> representing as a string. So the following code tries to sketch this:
>
> (require xml)
>
> ;; Xexpr -> String
> (define (xexpr->xml/pretty x)
> (with-output-to-string
>(lambda ()
> (display-xml/content
> (xexpr->xml x)
>
> (define x
> `(html
> (body ((bgcolor "red"))
> "Hi!" (br) "Bye!")))
>
> (xexpr->xml/pretty x) ;; turn XML into string with newlines and indentation
> and all — but see how the IDE renders it in the REPL
>
> (displayln (xexpr->xml/pretty x)) ;; print the same string — now the IDE is
> forced to render the printed output
>
> — Matthias
>
>
>
>
> > On May 28, 2017, at 7:55 AM, Zelphir Kaltstahl
> > wrote:
> >
> > I have some HTML code, which I generate using x-expressions:
> >
> > (string-append DOCTYPE-HTML5
> > "\n"
> > (xexpr->string
> >`(html
> > (body ((bgcolor "red"))
> >"Hi!" (br) "Bye!"
> >
> > I don't like how the result looks in the source code of the web page
> > created in this way. I'd like there to be indentation and line breaks,
> > according to the nesting of the DOM elements.
> >
> > So far I've found the following links and information:
> >
> > - https://docs.racket-lang.org/reference/pretty-print.html (I don't know
> > how to use it, because it has no examples. It also seems more like a
> > library, which generically can pretty print anything you want, provided you
> > "configure" is correctly. However, HTML is quite complex, I think, so I'd
> > not like to rewrite something that already exists elsewhere.)
> > - https://docs.racket-lang.org/xml/ (Here are some mentions of indentation
> > and newlines, but I need some procedure, which returns it, not displays it
> > immediately and then "does not let me have it", or is there a way to
> > somehow redirect output into a value which I can use? For example
> > `display-xml` seems to do what I need. :)
> >
> > Ideal might be a keyword parameter for `xexpr->string`, which specifies
> > whether it should prettify the string or not, but such a thing does not
> > exist according to the docs.
> >
> > How can I prettify my HTML output?
> >
> > (I really don't care about a few bytes more data needing to be sent. This
> > is not the next Google.)
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
Perfect! Does exactly what I wanted.
It is only that I care how it looks when I open the source code view of a web
page and all the HTML was on one super long line. I know it does not make a
difference for how the browser renders things, but it is somewhat annoying for
myself to know that it is not properly indented etc. in the source of the HTML
page.
I did not know about `with-output-to-string` - This means I can get any output
of a procedure, which usually prints that right away, into a string, I guess.
Thanks for your help.
--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.