{"size": 1784, "ext": "scrbl", "max_stars_repo_path": "polyglot-doc/polyglot/scribblings/how-tos/embed-code-examples.scrbl", "max_stars_repo_name": "pmatos/polyglot", "max_stars_repo_stars_event_min_datetime": "2019-08-25T10:41:17.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-03T19:33:04.000Z", "max_issues_repo_path": "polyglot-doc/polyglot/scribblings/how-tos/embed-code-examples.scrbl", "max_issues_repo_name": "pmatos/polyglot", "max_issues_repo_issues_event_min_datetime": "2019-08-25T10:44:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-03T12:22:16.000Z", "max_forks_repo_path": "polyglot-doc/polyglot/scribblings/how-tos/embed-code-examples.scrbl", "max_forks_repo_name": "pmatos/polyglot", "max_forks_repo_forks_event_min_datetime": "2019-12-19T06:18:49.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-13T03:20:07.000Z"}
{"max_stars_count": 89, "max_issues_count": 29, "max_forks_count": 5, "avg_line_length": 33.6603773585, "max_line_length": 96, "alphanum_fraction": 0.658632287}
#lang scribble/manual

@require[@for-label[racket polyglot] "../macros.rkt"]

@title[#:tag "embed-code-examples"]{How-To: Embed Working Code Examples Using App Element Loops}

Built-in workflows will not consider a page "done" until all app elements
and lib elements are gone. You can leverage this to iterate on content.
Obviously this comes with a risk of an infinite loop, but that's loops for
you.

One useful example is in showing code to the end-user, plus the actual
output of the same code.

@section{Imperative Workflow}

In @(imperative-workflow), all you need to do to make a working code example
is print the code in a preformatted block along with a new app element
containing the same code. It's fine if each line of code is a seperate
child node of the script element. The lines will be joined with newline
characters internally.

@racketmod[#:file "<script type=\"application/racket\">...</script>"
racket/base
(define code '("#lang racket"
               "(write `(h1 \"Hello, meta.\"))"))

(write `(pre . ,code))
(write `(script ((id "example") (type "application/racket")) . ,code))
]

@section{Functional Workflow}

An equivalent working code example for the Functional Workflow is as follows:

@racketmod[#:file "<script type=\"application/racket\">...</script>"
racket/base

(require polyglot)
(provide replace-page)

(define code '("#lang racket/base"
               "(provide replace-page)"
               "(require polyglot)"
               "(define (replace-page page-tx)"
               "  (tx-replace-me page-tx"
               "              (λ (x) '((h1 \"Hello, meta!\")))))"))

(define (replace-page page-tx)
  (tx-replace-me page-tx
                 (λ (x) `((pre . ,code)
                          (script ((type "application/racket")) . ,code)))))]
