Debian Codegraph @

vendor/mck89/peast/doc/rendering.md

raw
1 Rendering
2 ==========
3 **_From version 1.2_**
4
5 To render an AST generated by Peast you need to create an instance of the **Renderer** class and associate a **Formatter** to it:
6 ```php
7 $source = "var a;";
8 //Generate the AST
9 $ast = Peast\Peast::latest($source, $options)->parse();
10 //Create the renderer
11 $renderer = new Peast\Renderer;
12 //Associate the formatter
13 $renderer->setFormatter(new Peast\Formatter\PrettyPrint);
14 //Render the AST
15 echo $renderer->render($ast); //"var a;"
16 ```
17
18 Formatters
19 -------------
20 A Formatter specifies how the Renderer must format the nodes to produce the output.
21 Peast implements 3 formatters: **PrettyPrint**, **Compact** and **Expanded**.
22
23 For example using this source code:
24 ```js
25 if (fn(param)) alert("Ok");
26 else alert("Fail");
27 ```
28
29 ##### PrettyPrint
30 Produces a well formatted version of the code.
31 ```js
32 if (fn(param)) {
33 alert("Ok");
34 } else {
35 alert("Fail");
36 }
37 ```
38
39 ##### Compact
40 Produces a compact version of the code by removing whitespaces and optional brackets.
41 ```js
42 if (fn(param))alert("Ok");else alert("Fail");
43 ```
44
45 ##### Expanded
46 An expanded version of PrettyPrint.
47 ```js
48 if ( fn( param ) )
49 {
50 alert( "Ok" );
51 } else
52 {
53 alert( "Fail" );
54 }
55 ```
56
57 Custom Formatter
58 -------------
59 Peast allows you to create your own formatter.
60 You can do it by creating a class that extends `Peast\Formatter\Base` class and overwriting its protected properties:
61
62 ```php
63 class MyFormatter extends Peast\Formatter\Base {
64 //Use Windows style line endings
65 protected $newLine = "\r\n";
66 }
67 $renderer = new Peast\Renderer;
68 $renderer->setFormatter(new MyFormatter);
69 echo $renderer->render($ast);
70 ```
71
72 Available properties are:
73 * `$newLine`: line separator string (default: `"\n"`)
74 * `$indentation`: indentation string (default: `"\t"`)
75 * `$newLineBeforeCurlyBracket`: if true, open curly brackets of code blocks will be put on a new line (default: `false`)
76 * `$alwaysWrapBlocks`: if true, curly brackets around code blocks will always be inserted, also when they are optional (default: `true`)
77 * `$spacesAroundOperators`: if true, a space will be inserted before and after operators (default: `true`)
78 * `$spacesInsideRoundBrackets`: if true, content inside round brackets will be surrounded by spaces (default: `false`)
79
80 Shortcut method
81 -------------
82 Every syntax node has its own `render` method that you can use as a shortcut.
83 For example:
84
85 ```php
86 $ast = Peast\Peast::latest($source, $options)->parse();
87 $ast->render(new Peast\Formatter\PrettyPrint);
88 //Equivalent to
89 $ast = Peast\Peast::latest($source, $options)->parse();
90 $renderer = new Peast\Renderer;
91 $renderer->setFormatter(new Peast\Formatter\PrettyPrint);
92 $renderer->render($ast);
93 ```
94
95 Comments rendering
96 -------------
97 **_From version 1.14_**
98 Comments can be rendered by passing `true` to the formatter constructor:
99 ```php
100 $ast = Peast\Peast::latest($source, array("comments" => true))->parse();
101 $ast->render(new Peast\Formatter\PrettyPrint(true));
102 ```
103 Note that comments can be rendered only when parser is enabled to collect them, to do this you must set the `comments` option to `true`.
104 Also note that only PrettyPrint and Expanded formatters allow comments rendering, while Compact does not allow it by default.