Semantically linking to code snippets

Posted by Tim on Stack Overflow See other posts from Stack Overflow or by Tim
Published on 2011-01-01T22:33:14Z Indexed on 2011/01/01 22:54 UTC
Read the original article Hit count: 261

What's the most simple and semantic way of presenting code snippets in HTML?


Possible XHTML syntax

<a href="code_sample.php" type="text/x-php">
  Example of widget creation
</a>


Example of linked file (code_sample.php):

// Create a new widget
$widget = new widget();


Pros:

  • Semantically uses title to describe the source code being referenced
  • Up to the client to render snippet
    • Having very 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)
  • Degradation: non-compliant browsers receive a link to the associated content

Cons:

  • Not semantic enough? Seems wrong to replace hyperlinks with source code for presentation
  • <object> might be better, but wouldn't degrade as nicely.


Background

I'm trying to create a "personal" XHTML standard for storing notes (wow, this is probably among the nerdiest things I've said). Since notes are just "scratch" it needs to be very lightweight. SO's markdown is very lightweight but not semantic enough for my needs. Plus, now I'm just curious.


What's the most ideal syntax for linking to client-rendered code-snippets?

© Stack Overflow or respective owner

Related posts about html

Related posts about language-agnostic