Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to embed one code snippet into another?

Let's assume that I have

  • snippet A
  • snippet B
    where snippet A contains snippet B n times with n > 1.

Right now I have copied the content of snippet B into snippet A. This has the disadvantage, that whenever I change snippet B, I have to additionally change snippet A. Therefore, my question is whether there is some kind of statement to embed one snippet into another?
e.g.
<externalsnippet src=".\snippetB.snippet" />
or something similar.

like image 355
licensedlice Avatar asked Aug 12 '10 11:08

licensedlice


People also ask

Can you embed code snippets in markdown?

Code Blocks To write longer or more detailed snippets of code, it is often better to place them inside a code block. Code blocks allow you to use multiple lines, and markdown will render it inside its own box and with code type font.

How do I copy code snippets?

An inline code snippet can be copied by clicking anywhere on the snippet itself. A single line code snippet can be copied by clicking on the “copy” icon. The browser also provides an ability to manually highlight the text and choose “copy” from the context menu (right click).

How do I add a code to snippet in HTML?

This is quick and easy to do using a tool like: http://htmlencode.net/ Use <pre><code> tags to surround your code. For the <pre> tag you can optionally add class=”line-numbers”. This will add line numbers to your code and these line numbers will not be copied if students copy and paste snippets.


1 Answers

You could use an external parsed general entity to declare an entity reference for snippet B and then use it n number of times inside of snippet A.

When snippet A is parsed, the entity references will be expanded and the content from snippet B will be included at each spot where the entity was used.

For example, assume that you had a file called snipppetB.xml:

<snippetB>
  <foo>Content goes here</foo>
</snippetB>

And a file for snippet A declared an entity called snippetB referencing snippetB.xml and used it four times:

<!DOCTYPE snippetA [
   <!ENTITY snippetB SYSTEM "./snippetB.xml">
]>
<snippetA>
<a>&snippetB;</a>
<b>&snippetB;</b>
<c>&snippetB;</c>
<d>&snippetB;</d>
</snippetA>

When snippetA.xml is parsed, the XML content would look like this:

 <snippetA>
 <a>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </a>
 <b>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </b>
 <c>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </c>
 <d>
 <snippetB>
  <foo>Content goes here</foo> 
  </snippetB>
  </d>
  </snippetA>
like image 80
Mads Hansen Avatar answered Oct 06 '22 08:10

Mads Hansen