Git Product home page Git Product logo

literate-programming's Introduction

Org-Mode, Babel and Literate Programming

Table of Contents

Org-Mode

What is org?

Org aims to organize you. Your notes, your tasks, your planning, all is handled best in Org.

Org often gets confused with a markup langauage. In reality it is a (major) ‘mode’. To those unfamiliar with Emacs a ‘mode’ defines the interaction aspects of a buffer. Every buffer has a single major mode and then as many minor-modes as the user desires. For example, to edit Web files Web-Mode can be used as the major mode (this would give you syntax highlighting, auto-completion support, etc), if Emmet support is also wanted, Emmet-mode (a minor mode) could then be turned on as well.

While many online services like github bitbucket are supporting ‘.org’ files and displaying them correctly, this is only half the picture, the other part is the interaction environment itself.

As a markup language.

Org has everything markdown has to offer such as:

  • Unordered lists (such as this one)
  • Ordered lists
  • Headlines
  • Links

And beyond

Org offers the concept of Todo’s

Todos

Any headline can be turned into a todo action item

And its status can be changed with Shift + Left or Right Arrow

Checkboxes

  • [ ] any item in a plain list can be turn into a checkbox
  • [ ] these can be interacted with using C-c C-c

Tables

So these aren’t impossible to create in Markdown, but the auto-formatting and spreadsheet capabilities are next-level. Simply type ‘|’between columns and hit <TAB> upon row completetion. Let’s look at these as spreadsheets:

Spreadsheets

C-c ? - Show coordinates C-c } - Show Grid C-c C-c - recalculate value (point over formula)
NameAge
Julian30
Paul34
Charles42
Total106

Tags

Words enclosed between : : on a headline are tags for that headline. For example, I have tagged ‘Todos’ and ‘Checkboxes’ with ‘task_management’.

Clock and time

Org supports time stamping anywhere with C-c . <2018-07-20 Fri>

I can hit C-c C-x C-i on it to start and then C-c C-x C-o to stop a clock on any item. Time will get logged to the logbook drawer.

Navigating and narrowing Org files

Tab folds and unfolds content. C-M variants of emacs navigation commands work across headline items.

Sparse trees can be generated to narrow down to the information you need using C-c /.

Why?

Lastly, let’s discuss why you might want to use this for notes. Project planning gets to remain in a plain-text format while providing the interactivity that other software uses as far an engaging with it. Why does plain text matter? Version control. Plain and simple. Project planning evolves as much as code does yet documents that are commonly ‘google drive’ docs/sheets don’t hold their history in the same way, don’t get commit logs and versioning is often hand-documented.

Org-Babel

What is org-babel?

Org babel allows the inclusion of source code blocks in your org file. This isn’t that same as enclosing code between 3 backticks as in Markdown, that merely displays code properly. Babel blocks are more like portals to other ‘major-modes’ that house actionable code.

function factorial (n) {
  function fact(n, acc) {
    if (n < 2) {
      return acc;
    } else {
      return fact(n-1, n * acc);
    }
  }

  return fact(n, 1)
}

console.log(factorial(5))

pressing C-c C-c in the above block actually runs and evaluates that code

‘tangling’ allows the blocks to be exported together. ‘exporting’ allows single blocks to dictate where they live (generated by org). variables can be used in each block and across block. And results from one block can be passed to one another.

Literate Programming

Furthur discussing org-babel is better suited with context.

What is literate programming?

Literate programming is a programming paradigm introduced by Donald Knuth in which a program is given as an explanation of the program logic in a natural language, such as English, interspersed with snippets of macros and traditional source code, from which a compilable source code can be generated.

That’s the boring definition and I’d be hard-pressed to find someone who would want to wrap their entire codebase in narrative prose. Let’s take about where Org really shines, README’s for project configuration.

Literate README’s

Many readmes include steps the developer has to take to get up and running. Lets take a look at the most common of these. How many times have you seen, clone the repo and run yarn… Now imagine the following:

Run the following command in shell by typing C-c C-c

yarn

Notice that we also get output in the document. Othertimes README’s may describe an API, or describe some endpoints. Forcing you elsewhere to actually test those endpoints.

Lets take a look at the following:

Press C-c C-c in the following block to execute the people API endpoint. Note the ID passed to ‘people’ that specifies which person (1, Luke Skywalker)

GET https://swapi.co/api/people/1

Here the results of that fetch are piped in as a variable to the next block.

console.log(JSON.parse(data.slice(data.indexOf('{'), data.lastIndexOf('}') + 1)).name);

The above block could be parsed in any babel-supported language, fed to any babel-supported language etc…

Languages of interest

js - javascript

shell - your default shell

ob-mongo - execute mongo queries in a code block

ob-typescript

ob-sql-mode

ob-browser - render html as images inline

gnuplot (from above) can be used as a language instead, allowing insertable results

reset

set title "Putting it All Together"

set xlabel "X"
set xrange [-8:8]
set xtics -8,2,8


set ylabel "Y"
set yrange [-20:70]
set ytics -20,10,70

f(x) = x**2
g(x) = x**3
h(x) = 10*sqrt(abs(x))

plot f(x) w lp lw 1, g(x) w p lw 2, h(x) w l lw 3

artist mode - can be used if the src is meant to be ASCII for notetaking purposes


this doesnt need to be evaulated, ANY language is support re: display and ‘mode’ interactions

plantuml mode - allows the creation of diagrams. the end result is both a nice visual representation and the ability to keep the document in question text-based.

Sequence Diagrams
Alice -> Bob: synchronous call
Alice ->> Bob: asynchronous call
Activity Diagrams
:Up first, do this;
:Then this;
Activity with Conditional
start

if (GraphViz installed?) then (yes)
 :process all\ndiagrams;
else (no)
:process only
  __sequence___and___activity___diagrams;
endif

stop
Use case diagram
@startuml
left to right direction
skinparam packageStyle rectangle
actor customer
actor clerk
rectangle checkout {
  customer -- (checkout)
  (checkout) .> (payment) : include
  (help) .> (checkout) : extends
  (checkout) -- clerk
}
@enduml

ditaa

+---------+            +-------------+
|{d}      |            | {s}         |
|  one    | -------->  |  two        |
|         |     |      |             |
+---------+     |      +-------------+
                |
                |
                |       +------------------+
                |       |   {io}           |
                +-------+  cRED            |
                        | some other stuff |
                        +------------------+

literate-programming's People

Contributors

vidjuheffex avatar

Watchers

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.