Export interactive PHP DocBlock snippets#258
Conversation
|
This makes all PHP-backtick fenced code examples runnable and editable? So the existing syntax highlighted code blocks are marked by extra indentation (I think they start with extra 4 spaces) and this form uses the php code fence. I imagine folks will gradually migrate to code fences, especially since it allows for different languages to be indicated. Not all the snippets we have are PHP, and not all are even code. What do you think of using a specific name for this like I looked at MDN and how they do different types of code examples. This doc is interesting.
|
sirreal
left a comment
There was a problem hiding this comment.
This is a great start. My thoughts:
- Use a special indicator for a runnable snippet, not just
php. - Be more strict about what's allowed, e.g. only one form of info like
setup-blueprintand notsetup_blueprint,setupblueprintetc.
The expected-output is interesting, but it's just informative, right? It would be awesome to leverage that as part of the source test suite as a check on whether these snippets run as expected.
|
I think we'd also want either global blueprints or a default blueprint. It seems like practically everything want |
It's supposed to be the default displayed output so that you can see the results without booting Playground. We could also use it in CI to keep all code examples working. |
I really like it, thank you for sharing! In particular, this syntax sounds like such a good idea: If we adopted it, it could be: I also like the metadata syntax 🤔 But it seems like too much. Maybe we could just do: |
f96f9fe to
ca7c852
Compare
|
I merged this (at 06ec2b1) with #250 and WordPress/wporg-developer#567. I made the changes below to the Core source to test the features and output. It works great! One of my examples fails because it doesn't load the WordPress environment. It seems like that should be default behavior for these examples. php -dmemory_limit=4G generate-json-manually.php -d /path/to/wordpress-develop/src -o /path/to/wporg/dev/env/wordpress-phpdoc.json
wp-env run cli wp -- parser import path/to/wordpress-phpdoc.json --user=1Core diffdiff --git i/src/wp-includes/html-api/class-wp-html-processor.php w/src/wp-includes/html-api/class-wp-html-processor.php
index b2a597b938..f96068e80d 100644
--- i/src/wp-includes/html-api/class-wp-html-processor.php
+++ w/src/wp-includes/html-api/class-wp-html-processor.php
@@ -138,6 +138,10 @@
* The parser does not implement the "maybe clone an option into selectedcontent" algorithm.
* SELECTEDCONTENT elements may not reflect the actual selected content.
*
+ * ```setup-blueprint require-wp
+ * {"steps":[{"step":"writeFile","path":"/wordpress/wp-content/mu-plugins/shared.php","data":"<?php require '/wordpress/wp-load.php';"}]}
+ * ```
+ *
* @since 6.4.0
*
* @see WP_HTML_Tag_Processor
@@ -5431,14 +5435,28 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
*
* This generator function is designed to be used inside a "foreach" loop.
*
- * Example:
+ * ```php
+ * <?php
+ * echo 'not really a demo';
+ * ```
*
- * $p = WP_HTML_Processor::create_fragment( "<div class='free <egg<\tlang-en'>" );
- * $p->next_tag();
- * foreach ( $p->class_list() as $class_name ) {
- * echo "{$class_name} ";
- * }
- * // Outputs: "free <egg> lang-en "
+ * ```php interactive
+ * <?php
+ * echo 'really a demo';
+ * ```
+ *
+ * ```php interactive setup-blueprint=require-wp
+ * <?php
+ * echo 'doing things';
+ * $p = WP_HTML_Processor::create_fragment( "<div class='free <egg<\tlang-en'>" );
+ * $p->next_tag();
+ * foreach ( $p->class_list() as $class_name ) {
+ * echo "{$class_name} ";
+ * }
+ * ```
+ * ```expected-output
+ * "free <egg> lang-en "
+ * ```
*
* @since 6.6.0 Subclassed for the HTML Processor.
*/ |
0db1f61 to
48099d1
Compare
Exploration work in progress for WordPress/wporg-developer#567.
Exports only PHP DocBlock fences explicitly marked
interactiveas structureddoc.code_snippetsdata:The accepted forms are exactly
php interactive,php interactive setup-blueprint=NAME,setup-blueprint,setup-blueprint NAME, andexpected-output. Plainphpfences remain ordinary documentation. Setup Blueprints must be JSON objects. The parser rejects malformed JSON, duplicate or shadowed definitions, ambiguous or unattached metadata, and unresolved references with source locations.File-level definitions are inherited by functions, classes, methods, properties, and hooks. Class-level definitions are inherited by their properties and methods. Each descendant copies only the definitions it references.
Interactive fences become indexed
<!-- wp-parser-code-snippet:N -->comments inlong_description. The correspondingcode_snippetsentry containstype,code, and optionalexpected_outputandblueprint; reusable definitions live insetup_blueprints. This lets the companion theme render snippets between the surrounding paragraphs instead of appending them afterward.Screenshot
Actual rendering through the companion
wporg-developerPR, including expected output and a shared setup Blueprint:Backward compatibility
This can land before the theme change. Existing DocBlocks do not use
php interactive, plain PHP fences remain inlong_description, and unconsumed placement comments are invisible.Testing
Generate parser JSON from DocBlocks containing plain and interactive PHP fences, expected output, inline and reusable setup Blueprints, nested Markdown indentation, and tag-looking PHP. Confirm the JSON preserves snippet order, object shapes, source text, and inherited definitions. Import it and open the corresponding reference page with the companion theme PR; confirm every snippet renders at its placement comment.