GithubHelp home page GithubHelp logo

sketchscript's Introduction

SketchScript: Literate Code for Storytelling

This file, and all other .litcoffee files in this repo, flip the idea of coding around. Instead of seeing a code file with readable comments for documentation, you are now reading an English document with runnable code blocks. Sections set apart after a blank line and indented by four or more spaces will be executed:

print = console.log                  # alias the output function for clarity
print 'Running README...'

However, anything after a hashtag is ignored, even if set apart and indented.

# print "This won't appear when running README.litcoffee"

SketchScript Bootstrapping

You will need node, npm, and coffeescript. Once you have node, do npm install -g coffeescript to get coffee and cake. Then, coffee README.litcoffee or cake bake will set up the node package!

To use the development server, you can run npm install now. This will download

devDependencies =
  brunch: '2.10.17'
  'coffee-script-brunch': '3.0.0'

that can be used to host a local web server.

Our package definition

Node/npm requires a package.json file, and we create and save ours right here!

Lets create a function called runme.

Everything indented after the arrow -> will be part of this new function that we are assigning to a variable called runme:

runme = ->
  exports.package =
    name: 'sketchscript'
    version: '1.0.0'
    description: 'literate programming playground'
    main: 'README.litcoffee'
    author: 'mdan'
    license: 'AGPL-3.0'
    scripts: arguments[0]              # this will be passed in when run
    devDependencies: devDependencies   # this was defined on line 12, above

# de-indenting here says we're done writing the 'runme' function

A note on modules and functions

The word 'exports' above lets us treat this file as a module that can be used to get the data and code that is in this file from another document.

We have alreday done this in another file (scripts.litcoffee in the tools directory) so that we can assign a variable here to reference the data there:

scripts = require './tools/scripts.litcoffee'

Now, scripts contains all the exported code from the scripts.litcoffee file.

Calling the function

Until we execute runme(), exports.package does not exist.

Let's execute it now, making sure to send, or pass in, our scripts varible as the first argument to the function call.

runme scripts

NOTES

  • It would also work with explicit parentheses, as runme(scripts). This is often helpful for spreading a function call over multiple lines of code.
  • Inside the function, the special arguments array will have the data from scripts in array index 0. We can access this data with square brackets: arguments[0].
  • We wouldn't have to use the arguments array if we defined the function with a name to call this argument, for example, with runme = (scriptSection) ->

Formatting data as a text string

Now that we have all the data stored in exports.package, let's make it into a string of characters using the build-in JSON object's stringify method:

print '...serializing to pretty-printed json...'

formattedJSON = JSON.stringify(exports.package, null, '  ')

The second argument is null to indicate that we want to include all properties, rather than just some, in our output text.

Writing package.json

In order for us to use our package.json, we must save this file using node's core fs library:

print '...writing to disk...'

fs = require('fs')
fs.writeFileSync('package.json', formattedJSON)

print '...ready to use npm commands!'

If you haven't already, now may be a good time to run this file with cake bake

sketchscript's People

Contributors

dmtaub avatar

Watchers

Jonathan Tran avatar  avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.