diff options
-rw-r--r-- | files/fr/_redirects.txt | 3 | ||||
-rw-r--r-- | files/fr/mdn/structures/live_samples/index.html | 251 | ||||
-rw-r--r-- | files/ru/_redirects.txt | 4 | ||||
-rw-r--r-- | files/ru/mdn/structures/live_samples/index.html | 248 |
4 files changed, 0 insertions, 506 deletions
diff --git a/files/fr/_redirects.txt b/files/fr/_redirects.txt index 70d833f50f..fc0d8ffcdb 100644 --- a/files/fr/_redirects.txt +++ b/files/fr/_redirects.txt @@ -3124,8 +3124,6 @@ /fr/docs/MDN/Contribute/Contenu/Code_lignesdirectrices /fr/docs/MDN/Guidelines/Code_guidelines /fr/docs/MDN/Contribute/Howto/Creer_un_exercice_interactif_pour_apprendre_le_web /fr/docs/MDN/Contribute/Howto/Create_an_interactive_exercise_to_help_learning_the_web /fr/docs/MDN/Contribute/Howto/convertir_code_pour_etre_direct /fr/docs/MDN/Contribute/Howto/Convert_code_samples_to_be_live -/fr/docs/MDN/Contribute/Structures /fr/docs/MDN/Structures -/fr/docs/MDN/Contribute/Structures/Exemples_live /fr/docs/MDN/Structures/Live_samples /fr/docs/MDN/Contribute/Structures/Macros /fr/docs/MDN/Structures/Macros /fr/docs/MDN/Contribute/Structures/Macros/Commonly-used_macros /fr/docs/MDN/Structures/Macros/Commonly-used_macros /fr/docs/MDN/Contribute/Structures/Tables_de_compatibilité /fr/docs/MDN/Structures/Compatibility_tables @@ -3135,7 +3133,6 @@ /fr/docs/MDN/Feedback /fr/docs/MDN/Contribute/Feedback /fr/docs/MDN/Guidelines/Code_lignesdirectrices /fr/docs/MDN/Guidelines/Code_guidelines /fr/docs/MDN/Kuma /fr/docs/MDN/Yari -/fr/docs/MDN/Structures/Exemples_live /fr/docs/MDN/Structures/Live_samples /fr/docs/MDN/Structures/Tables_de_compatibilité /fr/docs/MDN/Structures/Compatibility_tables /fr/docs/MDN/User_guide /fr/docs/conflicting/MDN/Tools /fr/docs/MDN_a_dix_ans /fr/docs/MDN/At_ten diff --git a/files/fr/mdn/structures/live_samples/index.html b/files/fr/mdn/structures/live_samples/index.html deleted file mode 100644 index 0227ca25bc..0000000000 --- a/files/fr/mdn/structures/live_samples/index.html +++ /dev/null @@ -1,251 +0,0 @@ ---- -title: Exemples live -slug: MDN/Structures/Live_samples -tags: - - Débutant - - Guide - - MDN - - MDN Meta -translation_of: MDN/Structures/Live_samples -original_slug: MDN/Structures/Exemples_live ---- -<div>{{MDNSidebar}}</div> - -<p><span class="seoSummary">MDN permet de transformer les exemples présentés dans les articles en exemples dit "live" qui permettent aux lecteurs de les voir en action. Les exemples live peuvent inclure du HTML, CSS et JavaScript. A noter, les exemples lives <em>ne sont pas intéractifs</em>; néanmoins, ils assurent que le code de sortie affiché pour un exemple est bien le même que défini par l'exemple, parce que la sortie est générée par le code d'exemple.</span></p> - -<h2 id="Comment_le_système_dexemples_lives_fonctionne-t-il">Comment le système d'exemples lives fonctionne-t-il ?</h2> - -<p>Le système d'exemple live rassemble tout le code existant et le fusionne dans un fichier HTML, puis affiche ce fichier dans une {{HTMLElement("iframe")}}. Un exemple live est composé de deux parties : </p> - -<ul> - <li>Un groupe qui comporte tout le code de l'exemple</li> - <li>La macro (crée l'<em>iframe</em> ou le lien qui) affiche le résultat du bloque de code</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>Note:</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><iframe></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="Live_sample_macros">Live sample macros</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="Using_the_live_sample_system">Using the live sample system</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="Prepare_the_code_samples">Prepare the code samples</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>Note:</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>Note:</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="Insert_the_live_sample_macro">Insert the live sample macro</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="Adding_a_new_live_sample">Adding a new live sample</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="Using_the_Sample_Finder">Using the Sample Finder</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">Finding samples that need updating</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="Finding_samples_to_turn_into_live_samples">Finding samples to turn into live samples</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">Live sample demo</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"><p>A simple example of the live sample system in action.</p> -<div class="box"> - <div id="item">Hello world!</div> -</div> -</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="Result">Result</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="Conventions_regarding_live_samples"><a id="conventions" name="conventions">Conventions regarding live samples</a></h2> - -<div class="note"> -<p><strong>Note:</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>Orders of code blocks</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>Naming of headings</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>Using a "Result" block</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> diff --git a/files/ru/_redirects.txt b/files/ru/_redirects.txt index 4a4f86924d..5b3db96228 100644 --- a/files/ru/_redirects.txt +++ b/files/ru/_redirects.txt @@ -309,9 +309,6 @@ /ru/docs/MDN/Contribute/Editor/Ссылки /ru/docs/orphaned/MDN/Editor/Links /ru/docs/MDN/Contribute/Howto/Добавить_или_обновить_данные_о_браузерной_совместимости /ru/docs/MDN/Contribute/Howto/Add_or_update_browser_compatibility_data /ru/docs/MDN/Contribute/Howto/Создай_интерактивное_упражнение_для_помощи_в_изучении_веба /ru/docs/MDN/Contribute/Howto/Create_an_interactive_exercise_to_help_learning_the_web -/ru/docs/MDN/Contribute/Structures /ru/docs/MDN/Structures -/ru/docs/MDN/Contribute/Structures/Live_samples /ru/docs/MDN/Structures/Live_samples -/ru/docs/MDN/Contribute/Structures/Live_samples/Simple_live_sample_demo /ru/docs/orphaned/MDN/Structures/Live_samples/Simple_live_sample_demo /ru/docs/MDN/Contribute/Tools /ru/docs/MDN/Tools /ru/docs/MDN/Contribute/Tools/Page_watching /ru/docs/orphaned/MDN/Tools/Page_watching /ru/docs/MDN/Contribute/Процессы /ru/docs/MDN/Contribute/Processes @@ -330,7 +327,6 @@ /ru/docs/MDN/Getting_started /ru/docs/MDN/Contribute/Getting_started /ru/docs/MDN/Kuma /ru/docs/MDN/Yari /ru/docs/MDN/Kuma/Troubleshooting_KumaScript_errors /ru/docs/MDN/Tools/KumaScript/Troubleshooting -/ru/docs/MDN/Structures/Live_samples/Simple_live_sample_demo /ru/docs/orphaned/MDN/Structures/Live_samples/Simple_live_sample_demo /ru/docs/MDN/Tools/Page_watching /ru/docs/orphaned/MDN/Tools/Page_watching /ru/docs/MDN/Tools/Search /ru/docs/orphaned/MDN/Tools/Search /ru/docs/MDN/Tools/URL-suffix /ru/docs/MDN/Tools/Unsupported_GET_API diff --git a/files/ru/mdn/structures/live_samples/index.html b/files/ru/mdn/structures/live_samples/index.html deleted file mode 100644 index 4d55e877bc..0000000000 --- a/files/ru/mdn/structures/live_samples/index.html +++ /dev/null @@ -1,248 +0,0 @@ ---- -title: Live samples -slug: MDN/Structures/Live_samples -tags: - - Guide - - Intermediate - - MDN Meta - - NeedsTranslation - - Structures - - TopicStub -translation_of: MDN/Structures/Live_samples ---- -<div>{{MDNSidebar}}</div> - -<p><span class="seoSummary">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="How_does_the_live_sample_system_work">How does the live sample system work?</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>Note:</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><iframe></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="Live_sample_macros">Live sample macros</h3> - -<p>There are two macros that you can use to display live samples:</p> - -<ul> - <li><span class="templateLink"><code><a href="https://github.com/mdn/kumascript/blob/master/macros/EmbedLiveSample.ejs">EmbedLiveSample</a></code></span> embeds a live sample into a page</li> - <li><span class="templateLink"><code><a href="https://github.com/mdn/kumascript/blob/master/macros/LiveSampleLink.ejs">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="https://github.com/mdn/kumascript/blob/master/macros/EmbedLiveSample.ejs">EmbedLiveSample</a></code></span> or <span class="templateLink"><code><a href="https://github.com/mdn/kumascript/blob/master/macros/LiveSampleLink.ejs">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="Using_the_live_sample_system">Using the live sample system</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="Prepare_the_code_samples">Prepare the code samples</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>Note:</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>Note:</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="Insert_the_live_sample_macro">Insert the live sample macro</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="Adding_a_new_live_sample">Adding a new live sample</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="Using_the_Sample_Finder">Using the Sample Finder</h3> - -<p>As mentioned above, the Sample Finder is activated by clicking the<strong> Insert Live Code Sample iFrame</strong> icon. Unfortunately 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">Finding samples that need updating</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="Finding_samples_to_turn_into_live_samples">Finding samples to turn into live samples</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">Live sample demo</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"><p>A simple example of the live sample system in action.</p> -<div class="box"> - <div id="item">Hello world! Welcome to MDN</div> -</div> -</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; - border-radius: 6px; - padding: 20px; - background-color: #ffaabb; -} - -#item { - font-weight: bold; - text-align: center; - font-family: sans-serif; - font-size: 1.5em; -} -</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="Result">Result</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="Conventions_regarding_live_samples"><a id="conventions" name="conventions">Conventions regarding live samples</a></h2> - -<dl> - <dt>Orders of code blocks</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>Naming of headings</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>Using a "Result" block</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> |