views:

80

answers:

3

I've been using Markdown recently.

One of my biggest problems with Markdown is that Markdown has no syntax for including files within a document (vs., say, the listings package for LaTeX).

I'd like to extend Markdown to support including whole and partial files as code snippets. For instance, it could look like this:

![:include src/foo/bar.rb](10-20)

and that would put the contents of bar.rb lines 10-20 into my document as a code block. The rationale is that

  • documentation can be updated as the code changes. (vs. copy and paste which always gets outdated)
  • you can then unit test the exact code that is in the documentation

My questions are:

  1. What should the syntax be?
  2. Has this already been done and I am missing it?
+1  A: 

I'd be more inclined to come up with a general means to extend Markdown syntax, and then use that to provide support for an include function. So for example you could define syntax like (I'm not really suggesting this particular syntax, just an example):

[[command: arg arg arg...]]

..where command refers to a command that the markdown parser doesn't understand, but can call back to something else to process it. Then you can build an include function that will work with markdown, but not actually be part of it. Something like:

[[include: src/foo/bar.md]]

Oh and if you do this, I'd probably not provide a means to include a partial file, at least not by using line numbers - as it means you have to go back and edit all the include calls if you change the length of the document, which actually makes reuse harder (if you could come up with a way to tag sections, that might work better).

Dan
* I like the idea of having a generic `command`. Great idea* Good point on using markers rather than line numbers. I'll have to think about how to make that work well* This syntax feels idiomatic of Markdown as well
Nate Murray
A: 

I am generally inclined to see if things can be made to work withing existing syntax in a reasonable way. Currently, the

    ![Example Photo](http://example.com/example.jpg)

syntax and its relatives are used to include an image in the text. In a similar vein,

    +[Generic Heading](http://example.com/heading.txt)

or

    +[Local Heading](file:///dir/a/b/c/example.txt)

can be used to include text. In this case, the text in the square brackets is just like the alt-text attribute for inline images: It contains a short, human comprehensible description of the file being included.

Using + is intuitive to me: It means add the contents of this file to this document here.

Sinan Ünür
I like the idea of trying to keep the same syntax as much as possible, but part of the issue here is that we dont "know" what the `Text to include from local file system` is. We want to load it from the file without putting it in the Markdown file itself. So there is a bit of a chicken-and-the-egg problem with this syntax.
Nate Murray
@Nate Murray: You misunderstood me. `+[Text to include from local file system]` is just like the `alt text` attribute. Use it as a `REMark` to give a description of what is being included.
Sinan Ünür
Ah, that makes more sense. Cool, thank you.
Nate Murray
A: 

I'm a little late, sorry. but restructuredText already supports this: http://docutils.sourceforge.net/docs/ref/rst/directives.html#including-an-external-document-fragment

Mica