Semantically linking to code snippets
Question
What's the most lightweight, semantic, 开发者_运维问答and over-all best way of referencing snippets of code within XHTML 1.1?
Claim
Snippets of code within HTML are not semantically proper. Snippets (ideally) would only be referenced or linked (using <object>
, <a>
, etc.), not embedded, escaped, and styled in full.
Rationale
- Let browsers render code snippets
- Having many custom server-side implementations tells me it should be standardized
- Browsers can have plug-ins for copy+paste, download, etc.
- Seems to me this is where it belongs (not in Javascript)
- Why not use
<code>
- Escaped code is ugly
- We render it for the client
- No semantic representation of the language itself (MIME type would be nice)
- Javascript is functionality, not presentation.
- Syntax needs to be portable (i.e. no proprietary functionality)
- No need for hacked-together solutions
Illustration
Resource Tag Actual resource Alternative format Image img An image located on server `alt` attribute Source code ? A file containing code `code` tag? `a` link?
Example solution
code_sample.php is in the same directory as an HTML page containing the following:
<a href="code_sample.php" type="text/x-php">
Example of widget creation
</a>
- Pros
- Semantically uses title to describe the source code being referenced
- Degradation: non-compliant browsers render a link to the associated content
- Also, LinkType seems like a potentially good attribute for specifying "Code Snippet"
- http://www.w3.org/TR/xhtml-modularization/abstraction.html#dt_LinkTypes
- Cons
- Not semantic enough? Seems wrong to replace hyperlinks with source code for presentation
<object>
might be better, but wouldn't degrade as nicely.
What's the most lightweight, semantic, and over-all best way of referencing snippets of code within XHTML 1.1?
精彩评论