Using ScriptCS for Literate programming / blogging?

34 views
Skip to first unread message

Steve Cooper

unread,
Jul 10, 2015, 6:36:10 AM7/10/15
to scri...@googlegroups.com
Hi,

I was thinking that CSX files and literate programming would go very nicely together for technical blogging. I maintain a C# blog and it'd be great to write technical posts as commented CSX files. I'm rolling around an idea for a project, but I don't want to reinvent the wheel if there's something out there that suits my needs.

 Here's an example of the kind of thing I was considering; 

    /* .Net decimals are a little weird -- the values 12.0m and 12.000m 
       seem to represent the same value; */

    ASSERT(12.0m == 12.0000m);

    /* But they aren't quite the same -- they store their decimal precision,
       too, which means their string representations are different; */

    ASSERT(Convert.ToString(12.0m) != Convert.ToString(12.000m))

    /* So here's a safe way to compare the string representations of them; */

    string GetSafeDecimalString(Decimal d) { 
   return string.Format("{0:0000000000000000.0000000000000000}", d); 
    }

    ASSERT(GetSafeDecimalString(12.0m) == GetSafeDecimalString(12.000m));

The idea being to be able to write a blog post entirely in a CSX file, then run a process to output your HTML and guarantee compiling, tested code.

Seems like something other people might have considered -- are there any projects I should know about before I reinvent the wheel? 

Thanks,

   Steve

Adam Ralph

unread,
Jul 13, 2015, 3:43:36 AM7/13/15
to scri...@googlegroups.com
Hi Steve,

If you write your blog post as a csx, then you can indeed use scriptcs to execute it to verify it works.

Personally I would probably then just dump the contents of the csx into a <code> block and use something like https://github.com/google/code-prettify to format it. 

Steve Cooper

unread,
Jul 13, 2015, 5:27:34 AM7/13/15
to scri...@googlegroups.com
Thanks, Adam. 

Sounds like it shouldn't be too hard to write something, then. That's good. And I'm guessing no-one's tried it before that you know of?

Thinking about it, it sounds like the processor should be written in CSX itself; so the end result will run like;

    > scriptcs.exe produceBlogPost.csx blogPostContent.csx
    compiling csx
    running tests...
    PASS test1
    PASS test2
    PASS test3
    produced blogPostContent.html

I'll have a go at writing it when I get some time...

Adam Ralph

unread,
Jul 14, 2015, 2:56:42 AM7/14/15
to scri...@googlegroups.com
As far as writing csx files and then putting them in a blog, yes, this will have been done before.

Generating a blog post HTML file from a csx, perhaps not, but I shouldn't think it would be too hard.

I don't think you'll necessarily be able to distinguish between different 'parts' of your blogPostContent.csx being executed. I think it will be more like:

> scriptcs produce.csx post.csx
Running 'post.csx'...
Generating 'post.html'...

In produce.csx you will probably just start a process with the command line scriptcs post.csx and check the exit code.

Glenn Block

unread,
Jul 14, 2015, 3:03:18 AM7/14/15
to Adam Ralph, scri...@googlegroups.com
Or just use a batch file which calls scriptcs for each part.

Steve Cooper

unread,
Jul 15, 2015, 6:51:55 AM7/15/15
to scri...@googlegroups.com
Is there any way to extend scriptcs or get the AST? I was thinking about this more and I think I want a core loop over the top-level elements in the program;

foreach(var codeElement in script,TopLevelCodeElements)
{
If (codeElement.IsComment)
...
I was thinking of using Roslyn, but it'd be crazy for me to waste any of the good work you guys do.

Adam Ralph

unread,
Jul 16, 2015, 8:45:50 AM7/16/15
to scri...@googlegroups.com
scriptcs currently does not give you any access to the AST.

If you wanted to, you could use Roslyn itself from a bootstrapping script, by installing the package and referencing the DLL, and then loading your target script via the Roslyn API but it feels a bit contrived. You wouldn't really be using scriptcs for much.
Reply all
Reply to author
Forward
0 new messages