| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <title>Blockly Demo: Headless</title> |
| 6 | <script src="../../blockly_compressed.js"></script> |
| 7 | <script src="../../blocks_compressed.js"></script> |
| 8 | <script src="../../python_compressed.js"></script> |
| 9 | <script src="../../msg/js/en.js"></script> |
| 10 | <style> |
| 11 | body { |
| 12 | background-color: #fff; |
| 13 | font-family: sans-serif; |
| 14 | } |
| 15 | h1 { |
| 16 | font-weight: normal; |
| 17 | font-size: 140%; |
| 18 | } |
| 19 | td { |
| 20 | vertical-align: top; |
| 21 | } |
| 22 | textarea { |
| 23 | width: 100%; |
| 24 | height: 20em; |
| 25 | } |
| 26 | </style> |
| 27 | </head> |
| 28 | <body> |
| 29 | <h1><a href="https://developers.google.com/blockly/">Blockly</a> > |
| 30 | <a href="../index.html">Demos</a> > Headless</h1> |
| 31 | |
| 32 | <p>This is a simple demo of generating Python code from XML with no graphics. |
| 33 | This might be useful for server-side code generation.</p> |
| 34 | |
| 35 | <table style="width: 100%"> |
| 36 | <tr> |
| 37 | <td style="width:50%"> |
| 38 | <textarea id="xml_input"> |
| 39 | <xml id="startBlocks" style="display: none"> |
| 40 | <block type="controls_if" inline="false" x="20" y="20"> |
| 41 | <mutation else="1"></mutation> |
| 42 | <value name="IF0"> |
| 43 | <block type="logic_compare" inline="true"> |
| 44 | <field name="OP">EQ</field> |
| 45 | <value name="A"> |
| 46 | <block type="math_arithmetic" inline="true"> |
| 47 | <field name="OP">MULTIPLY</field> |
| 48 | <value name="A"> |
| 49 | <block type="math_number"> |
| 50 | <field name="NUM">6</field> |
| 51 | </block> |
| 52 | </value> |
| 53 | <value name="B"> |
| 54 | <block type="math_number"> |
| 55 | <field name="NUM">7</field> |
| 56 | </block> |
| 57 | </value> |
| 58 | </block> |
| 59 | </value> |
| 60 | <value name="B"> |
| 61 | <block type="math_number"> |
| 62 | <field name="NUM">42</field> |
| 63 | </block> |
| 64 | </value> |
| 65 | </block> |
| 66 | </value> |
| 67 | <statement name="DO0"> |
| 68 | <block type="text_print" inline="false"> |
| 69 | <value name="TEXT"> |
| 70 | <block type="text"> |
| 71 | <field name="TEXT">Don't panic</field> |
| 72 | </block> |
| 73 | </value> |
| 74 | </block> |
| 75 | </statement> |
| 76 | <statement name="ELSE"> |
| 77 | <block type="text_print" inline="false"> |
| 78 | <value name="TEXT"> |
| 79 | <block type="text"> |
| 80 | <field name="TEXT">Panic</field> |
| 81 | </block> |
| 82 | </value> |
| 83 | </block> |
| 84 | </statement> |
| 85 | </block> |
| 86 | </xml> |
| 87 | </textarea> |
| 88 | </td> |
| 89 | <td> |
| 90 | </td> |
| 91 | <td style="width:50%"> |
| 92 | <textarea id="code_output" readonly></textarea> |
| 93 | </td> |
| 94 | </tr> |
| 95 | </table> |
| 96 | |
| 97 | <div style="text-align: center"> |
| 98 | <button onclick="generate()">Generate Python ⤴</button> |
| 99 | </div> |
| 100 | |
| 101 | <script> |
| 102 | function generate() { |
| 103 | // Parse the XML into a tree. |
| 104 | var xmlText = document.getElementById('xml_input').value; |
| 105 | try { |
| 106 | var xml = Blockly.Xml.textToDom(xmlText) |
| 107 | } catch (e) { |
| 108 | alert(e); |
| 109 | return; |
| 110 | } |
| 111 | // Create a headless workspace. |
| 112 | var workspace = new Blockly.Workspace(); |
| 113 | Blockly.Xml.domToWorkspace(workspace, xml); |
| 114 | var code = Blockly.Python.workspaceToCode(workspace); |
| 115 | document.getElementById('code_output').value = code; |
| 116 | } |
| 117 | </script> |
| 118 | |
| 119 | </body> |
| 120 | </html> |