aboutsummaryrefslogtreecommitdiff
path: root/files/pt-pt/mdn/structures/live_samples/index.html
blob: 91295fc37c874b0fb96fb84b47ab8b8f4d518ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
---
title: Exemplos live
slug: MDN/Structures/Exemplos_live
tags:
  - Estruturas
  - Guia(2)
  - Intermediário
  - Metadados MDN
translation_of: MDN/Structures/Live_samples
---
<div>{{MDNSidebar}}</div><p><span class="seoSummary">Exemplos MDN supports turning sample code displayed in articles into running samples the reader can look at in action. These live samples can include HTML, CSS, and JavaScript in any combination.</span> Note that "live" samples are <em>not interactive</em>; however, they do ensure that the output displayed for a sample matches the code of the sample exactly, because it is actually generated by the code sample.</p>

<h2 id="Como_é_que_os_exemplos_live_funcionam">Como é que os exemplos <em>live </em>funcionam</h2>

<p>The live sample system gathers up all the code in a group, merges it into one HTML file, and then renders that HTML in an {{HTMLElement("iframe")}}. A live sample therefore consists of two pieces:</p>

<ul>
 <li>A group of code blocks</li>
 <li>The macro call that (creates the frame or the link that) displays the result of the code blocks</li>
</ul>

<p>A "group" of code blocks, in this context, is identified by the ID of a heading or a block element (such as a {{HTMLElement("div")}}).</p>

<ul>
 <li>If the ID belongs to a block element, the group includes all the code blocks within the enclosing block element whose ID is used. </li>
 <li>If the ID belongs to a heading, the group includes all the code blocks that are after that heading and before the next heading of the same heading level. Note that code blocks under subheadings of the specified heading are all used; if this is not the effect you want, use an ID on a block element instead.</li>
</ul>

<p>The macro uses a special URL to fetch the sample code for a given group: <code>http://<em>url-of-page</em>$samples/<em>group-id</em></code>, where <code>group-id</code> is the ID of the heading or block where the code is located. The resulting frame (or page) is sandboxed, secure, and technically may do anything that works on the Web. Of course, as a practical matter, the code must contribute to the point of the page that contains it; random stuff running on MDN will be removed by the editor community.</p>

<div class="note">
<p><strong>Nota:</strong> You <strong>must</strong> use the macro for presenting the live sample's output. MDN's editor will strip out any direct use of the <code>&lt;iframe&gt;</code> element in order to ensure security.</p>
</div>

<p>Each {{HTMLElement("pre")}} block containing code for the sample has a class on it that indicates whether it's HTML, CSS, or JavaScript code; these are "brush: html", "brush: css", and "brush: js". These classes must be on the corresponding blocks of code so that the wiki can use them correctly; fortunately, these are added for you automatically when you use the syntax highlighter features in the editor's toolbar.</p>

<p>The live sample system has lots of options available, and we'll try to break things down to look at them a bit at a time.</p>

<h3 id="Macros_de_exmplo_live">Macros de exmplo <em>live</em></h3>

<p>There are two macros that you can use to display live samples:</p>

<ul>
 <li><span class="templateLink"><code><a href="/en-US/docs/Template:EmbedLiveSample">EmbedLiveSample</a> </code></span>embeds a live sample into a page</li>
 <li><span class="templateLink"><code><a href="/en-US/docs/Template:LiveSampleLink">LiveSampleLink</a></code></span> creates a link that opens the live sample in a new page</li>
</ul>

<p>In many cases, you may be able to add the <span class="templateLink"><code><a href="/en-US/docs/Template:EmbedLiveSample">EmbedLiveSample</a></code></span> or <span class="templateLink"><code><a href="/en-US/docs/Template:LiveSampleLink">LiveSampleLink</a></code></span> macro to pages with little or no additional work! As long as the sample can be identified by a heading's ID or is in a block with an ID you can use, simply adding the macro should do the job.</p>

<h4 id="EmbedLiveSample_macro">EmbedLiveSample macro</h4>

<pre class="syntaxbox notranslate"> \{{EmbedLiveSample(<em>block_ID</em>, <em>width</em>, <em>height</em>, <em>screenshot_URL</em>, <em>page_slug</em>)}}</pre>

<dl>
 <dt>block_ID</dt>
 <dd>Required: The ID of the heading or enclosing block to draw the code from. The best way to be sure you have the ID right is to look at the URL of the section in the page's table of contents.</dd>
 <dt>width</dt>
 <dd>The width of the {{HTMLElement("iframe")}} to create, specified in <code>px</code>. This is optional; a reasonable default width will be used if you omit this. Note that if you want to use a specific width, you <em>must</em> also specify the height parameter.</dd>
 <dt>height</dt>
 <dd>The height of the {{HTMLElement("iframe")}} to create, specified in <code>px</code>. This is optional; a reasonable default height will be used if you omit this. Note that if you want to use a specific height, you <em>must</em> also specify the width parameter. If you use only one of them, the default frame size is used.</dd>
 <dt>screenshot_URL</dt>
 <dd>The URL of a screenshot that shows what the live sample should look like. This is optional, but can be useful for new technologies that may not work in the user's browser, so they can see what the sample would look like if it were supported by their browser. If you include this parameter, the screenshot is shown next to the live sample, with appropriate headings.</dd>
 <dt>page_slug</dt>
 <dd>The slug of the page containing the sample; this is optional, and if it's not provided, the sample is pulled from the same page on which the macro is used.</dd>
</dl>

<ol>
</ol>

<h4 id="LiveSampleLink_macro">LiveSampleLink macro</h4>

<pre class="syntaxbox notranslate"> \{{LiveSampleLink(<em>block_ID</em>, <em>link_text</em>)}}</pre>

<dl>
 <dt>block_ID</dt>
 <dd>The ID of the heading or enclosing block to draw the code from. The best way to be sure you have the ID right is to look at the URL of the section in the page's table of contents.</dd>
 <dt>link_text</dt>
 <dd>A string to use as the link text.</dd>
</dl>

<h2 id="Utilizar_o_sistema_de_exemplo_live">Utilizar o sistema de exemplo <em>live</em></h2>

<p>The sections below describe a few common use cases for the live sample system.</p>

<p>In all of these cases, to see the results of the live sample, you must click <strong>Save Changes</strong> in the editor (which takes you out of edit mode). Because of the reflexive, <a href="http://www.imdb.com/title/tt1375666/?ref_=nv_sr_1"><em>Inception</em></a>-like nature of live samples, the <strong>Preview Changes</strong> functionality is <em>not</em> able to display live samples.</p>

<h3 id="Turning_snippets_into_live_samples">Turning snippets into live samples</h3>

<p>One common use case is to take existing code snippets already shown on MDN and turn them into live samples.</p>

<h4 id="Preparar_os_exemplos_de_código">Preparar os exemplos de código</h4>

<p>The first step is to either add code snippets or ensure that existing ones are ready to be used as live samples, in terms of the content and in terms of their mark-up. The code snippets, taken together, must comprise a complete, runnable example. For example, if the existing snippet shows only CSS, you might need to add a snippet of HTML for the CSS to operate on.</p>

<p>Each piece of code must be in a {{HTMLElement("pre")}} block, with a separate block for each language, properly marked as to which language it is. Most of the time, this has already been done, but it's always worth double-checking to be sure each piece of code is configured with the correct syntax. Next to the <strong>PRE</strong> icon in the toolbar is a drop-down menu icon (tooltip: Syntax Highlighter) with the various languages that MDN does syntax highlighting for. Setting the language for the block for syntax highlighting also correlates it with a language for the purposes of the live sample system.</p>

<div class="note">
<p><strong>Nota:</strong> You may have more than one block for each language; they are all concatenated together. This lets you have a chunk of code, followed by an explanation of how it works, then another chunk, and so forth. This makes it even easier to produce tutorials and the like that utilize live samples interspersed with explanatory text.</p>
</div>

<p>So make sure the {{HTMLElement("pre")}} blocks for your HTML, CSS, and/or JavaScript code are each configured correctly for that language's syntax highlighting, and you're good to go.</p>

<div class="note">
<p><strong>Nota:</strong> When pasting content into MDN, please be aware that if pasting styled content (including, for example, syntax highlighting in code being copied from another site) that you may be bringing along unwanted and unneeded additional styles or classes. Please be careful not to do this; if necessary, review your edit in source mode to remove these unnecessary styles and classes (or check it before pasting, or use the "Paste as plain text" option instead).</p>
</div>

<h4 id="Inserir_a_macro_de_exemplo_live">Inserir a macro de exemplo <em>live</em></h4>

<p>Once the code is in place and properly configured to identify each block's language, you need to insert the macro that creates the iframe.</p>

<ol>
 <li>Click the <strong>Insert Live Code Sample iFrame</strong> button in the toolbar; it looks like this: <img alt="" src="https://mdn.mozillademos.org/files/5383/insert-live-sample-btn.png" style="height: 18px; width: 19px;">. This opens a dialog box for configuring your live sample frame:<img alt="" src="https://mdn.mozillademos.org/files/5385/sample-finder.png" style="height: 358px; width: 405px;"></li>
 <li>Under <strong>Document</strong>, enter the title of the article that contains the sample you wish to embed. By default, it's the article you're currently editing, but you can choose an article elsewhere on MDN, too. This makes it possible to reuse samples on multiple pages if needed. (If you type new text in this field, a menu of partially matching pages appears; select the title of the page you want.)</li>
 <li>In the <strong>Sections in Document</strong> menu, select the section in the article that contains the code blocks of the sample you want to embed.</li>
 <li>Click the <strong>OK</strong> button to generate and insert the macro call that creates the sample frame for you. The macro call looks something like this:<br>
  <strong>\{{ EmbedLiveSample('Live_sample_demo') }}</strong></li>
</ol>

<h3 id="Adicionar_um_novo_exemplo_live">Adicionar um novo exemplo <em>live</em></h3>

<p>If you're writing a new page, and want to insert code that you want to present as a live sample, even more of the work can be done for you by the editor! </p>

<ol>
 <li>Click the <strong>Insert Code Sample Template</strong> button in the toolbar, which looks like this: <img alt="" src="/files/4265/live-sample-button.png" style="height: 19px; width: 21px;">. This presents a simple dialog asking you to name your live sample:<br>
  <img alt="" src="https://mdn.mozillademos.org/files/5387/insert-live-sample-template.png" style="height: 155px; width: 251px;"></li>
 <li>Enter the title of the sample; this is used as the heading for the sample. Make sure that your title makes sense within the context of the page you're on.</li>
 <li>Click <strong>OK</strong>. A new heading with the title you entered is created, with sub-headings and empty code blocks for HTML, CSS, and JavaScript.</li>
 <li>Delete any headings and code blocks you don't need.</li>
 <li>Enter the code that makes up your sample in the appropriate code blocks</li>
 <li><a href="#conventions">Check conventions</a></li>
</ol>

<h3 id="Utilizar_o_Localizador_de_Exemplo">Utilizar o 'Localizador de Exemplo'</h3>

<p>As mentioned above, the Sample Finder is activated by clicking the<strong> Insert Live Code Sample iFrame</strong> icon. Unfortunately the the Sample Finder may produce a macro that is NOT usable without editing. There are two problem areas that should be carefully checked and edited if necessary.</p>

<ol>
 <li><strong>Document </strong>field. This field will search as you type and present a list of documents that match your string. But the list presented will NOT include the sub-page. For example, say you are working on the subpage for <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@counter-style/negative">Negative </a>under the main page <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@counter-style">@counter-style</a>. The Sample Finder search will not find Negative but will find the main page @counter-style. When @counter-style is selected the field background turns green. See below for the issue this creates.</li>
 <li><strong>Sections in Document</strong>. The pull-down menu Sections in Document may not show the section that you need. Just pick one, say Examples, and it can be fixed with a simple edit.</li>
</ol>

<p>Here is what the Sample Finder produced:</p>

<pre class="notranslate"><code>\{{ EmbedLiveSample('Examples', '', '', '', 'Web/CSS/@counter-style') }}</code></pre>

<p>This macro will not work. The block_ID is Examples and it should be Example in this case (check the source ID for this section to verify which block_ID you need to use. Similarly the page_slug is @counter-style and it should be @counter-style/negative. These corrections can be done directly in the page with Edit active.</p>

<p>After editing the macro now looks like this:</p>

<pre class="notranslate"><code>\{{ EmbedLiveSample('Example', '', '', '', 'Web/CSS/@counter-style/negative') }}</code></pre>

<p>This edited macro will work correctly. If the macro is working correctly you will see the <strong>Open in CodePen</strong> button. A thumbnail of the example should appear above the Open in CodePen button. If the button is there but there isn't a thumbnail, just wait a few minutes. It may take some time for the server to generate it.</p>

<h2 id="Live_sample_demo" name="Live_sample_demo">Encontrar exemplos que precisam de ser atualizados</h2>

<p>When looking for existing samples to update, there are three main kinds of updating you may wish to do:</p>

<ul>
 <li>Turn an existing non-live example snippet into a live sample.</li>
 <li>Correct bugs in an existing live sample.</li>
 <li>Improve an existing live sample, or update a sample based on technology changes.</li>
</ul>

<div class="note">
<p><strong>Note</strong><strong>:</strong> If you find an article that has samples in need of being updated to use the live sample system, please add the tag "NeedsLiveSample" to the page.</p>
</div>

<h3 id="Encontrar_exemplos_para_os_transformnar_em_exemplos_live">Encontrar exemplos para os transformnar em exemplos <em>live</em></h3>

<p>MDN has lots of older examples that don't yet use the live sample system. Our goal is to update most or all of these to be live samples. This will improve consistency and usability. You will almost certainly find many of these as you use MDN on a daily basis; however, how can you find them if you're specifically looking for them to update? Unfortunately, there's not an easy way to do that. But there are some guidelines you can follow to help track them down:</p>

<ul>
 <li>Start by looking at this <a href="/en-US/docs/tag/NeedsLiveSample" title="/en-US/docs/tag/NeedsLiveSamples">list of pages tagged with "NeedsLiveSample"</a>. These are pages that users have marked as needing updating. You should add this tag yourself, if you see a page that needs to be updated to use live samples but don't have time to fix right away.</li>
 <li>Browse through documentation trees that are likely to include examples, such as the <a href="/en-US/docs/Web/JavaScript/Guide" title="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a>, <a href="/en-US/docs/Web/HTML" title="/en-US/docs/Web/HTML">HTML documentation</a>, and <a href="/en-US/docs/Web/CSS/Reference" title="/en-US/docs/Web/CSS/Reference">CSS reference</a>.</li>
 <li>Search for terms like "<a href="/en-US/search?q=example" title="/en-US/search?q=example">example</a>" or "<a href="/en-US/search?q=sample" title="/en-US/search?q=sample">sample</a>" and look through the results for pages to update.</li>
</ul>

<h2 id="Live_sample_demo" name="Live_sample_demo">Demonstração de exemplo <em>live</em></h2>

<p>This section is the result of using the live sample template button to create not only the main heading ("Live sample demo"), but also subheadings for our HTML, CSS, and JavaScript content. You're not limited to one block of each; in addition, they don't even need to be in any particular order. Mix and match!</p>

<p>You may choose to delete any of these you wish; if you don't need any script, just delete that heading and its {{HTMLElement("pre")}} block. You can also delete the heading for a code block ("HTML", "CSS", or "JavaScript"), since these are not used by the live sample macro.</p>

<p>Now that the template has been inserted, we can put in some code, and even some explanatory text.</p>

<h3 id="HTML">HTML</h3>

<p>This HTML creates a paragraph and some blocks to help us position and style a message.</p>

<pre class="brush: html notranslate">&lt;p&gt;A simple example of the live sample system in action.&lt;/p&gt;
&lt;div class="box"&gt;
  &lt;div id="item"&gt;Hello world!&lt;/div&gt;
&lt;/div&gt;
</pre>

<h3 id="CSS">CSS</h3>

<p>The CSS code styles the box as well as the text inside it.</p>

<pre class="brush: css notranslate">.box {
  width: 200px;
  height: 100 px;
  border-radius: 6px;
  background-color: #ffaabb;
}

#item {
  width: 100%;
  font-weight: bold;
  text-align: center;
  font-size: 2em;
}
</pre>

<h3 id="JavaScript">JavaScript</h3>

<p>This code is very simple. All it does is attach an event handler to the "Hello world!" text that makes an alert appear when it is clicked.</p>

<pre class="brush: js notranslate">var el = document.getElementById('item');
el.onclick = function() {
  alert('Owww, stop poking me!');
}
</pre>

<h3 id="Resultado">Resultado</h3>

<p>Here is the result of running the code blocks above via<code> \{{EmbedLiveSample('Live_sample_demo')}}</code>:</p>

<p>{{EmbedLiveSample('Live_sample_demo')}}</p>

<p>Here is a link that results from calling these code blocks via <code>\{{LiveSampleLink('Live_sample_demo', 'Live sample demo link')}}</code>:</p>

<p>{{LiveSampleLink('Live_sample_demo', 'Live sample demo link')}}</p>

<h2 id="Convenções_sobre_exemplos_live"><a id="conventions" name="conventions">Convenções sobre exemplos <em>live</em></a></h2>

<div class="note">
<p><strong>Nota:</strong> This is currently (Feb. 2016) under discussion on the dev-mdc mailing list (see <a href="https://groups.google.com/forum/#!topic/mozilla.dev.mdc/49oqJAHFnWQ">this thread</a>). Any input is welcome. If this note persists after some month (Apr. 2016), please reach the author of the first email in this thread for updating this page.</p>
</div>

<dl>
 <dt>Ordem dos blocos de código</dt>
 <dd>When adding a live sample, the code blocks should be sorted so that the first one corresponds to the main language for this sample (if there is one). For example, when adding a live sample for the HTML Reference, the first block should be HTML, when adding a live sample for the CSS Reference, it should be CSS and so on.</dd>
 <dt>Nomeação de cabeçalhos</dt>
 <dd>When there is no ambiguity (e.g. the sample is under a "Examples" section), headings should be straightforward with the sole name of the corresponding language: HTML, CSS, JavaScript, SVG, etc. (see above). Headings like "HTML Content" or "JavaScript Content" should not be used. However if such a short heading makes content unclear, one can use a more thoughtful title.</dd>
 <dt>Utilizar um bloco de "Resultado"</dt>
 <dd>After the different code blocks, please use a last "Result" block before using the <code>EmbedLiveSample</code> macro (see above). This way, the semantic of the example is made clearer for both the reader and any tools that would parse the page (e.g. screen reader, web crawler).</dd>
</dl>