Giter Club home page Giter Club logo

literate-programming's Introduction

Literate Programming for Ruby and Markdown!

Let us Literate Programming!

Installation

Add this line to your application's Gemfile:

gem 'literate-programming'

And then execute:

$ bundle

Or install it yourself as:

$ gem install literate-programming

Installation from repository

You are also able to install this gem from github repository.

$ git clone https://github.com/pixie-grasper/literate-programming.git
$ cd literate-programming
$ rake install

Usage

  • How to Use?
$ rtangle example.wrb # to get example.rb
$ rweave example.wrb # to get example.md

Examples

Hello, world!

First, write hellow.wrb

First, define the Hello, world!
[[hello-world]] =
  "Hello, world!"

Next, define Main Program; 'Hello, world!'
[[*]] =
  def main
    p [[hello-world]]
  end

Finaly, call the main.
[[*]] +=
  main

Second, tangle it!

$ rtangle hello.wrb

Finally, you will get hello.rb

def main
  p "Hello, world!"
end

main

99 Bottles

This gem provides the Template Meta Programming. For example, below code is an implementation of the 99-bottles.

First, define the main function and it sings a song ''99 bottles of beer''.
[[*]] =
  def main
    [[bottles:100]]
  end

General case, sing below
If the label-name end with ':@', the rtangle accepts the codes as a general template.
In the general template code,
- if '@'-mark followed by a number, the rtangle replaces it with an argument.
- if '@@'-mark followed by parents, the rtangle replaces it with return value of the insides.
[[bottles:@]] =
  puts '@0 bottles of beer on the wall, @0 bottles of beer.'
  puts 'Take one down and pass it around, @@(@0 - 1) bottles of beer on the wall.'
  [[bottles:@@(@0 - 1)]]

The template code is also able to specialize.
If the rtangle does not find the specialized one,
it specializes from general template code like above.
When number of bottles == 2
[[bottles:2]] =
  puts '2 bottles of beer on the wall, 2 bottles of beer.'
  puts 'Take one down and pass it around, 1 bottle of beer on the wall.'
  [[bottles:1]]

When number of bottles == 1
[[bottles:1]] =
  puts '1 bottle of beer on the wall, 1 bottle of beer.'
  puts 'Take one down and pass it around, no more bottles of beer on the wall.'
  [[bottles:0]]

When no rest bottles...
[[bottles:0]] =
  puts 'No more bottles of beer on the wall, no more bottles of beer.'
  puts 'Go to the store and buy some more, 99 bottles of beer on the wall.'

Finally, call the main function.
[[*]] +=
  main

More Meta Method

This gem also provides lisp-like macro. Use *before* label to define functions that returns eval-able string.

If you want to do something that requires many sentence,
you can use *before* label;
It will be expanded and be evaluated by rtangle to help to write.
[[*]] =
  def main
    @@(helper_function)
  end

For example, *before* label likes below;
Note: the *before*before* label, the *before*before*before* label, and so on, are also exists.
[[*before*]] =
  def helper_function
    return "p 'Hello, world!'"
  end

Finally, call the main function.
[[*]] +=
  main

License

The gem is available as open source under the terms of the MIT License.

literate-programming's People

Contributors

pixie-grasper 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.