1. WEIRD STUFF WITH KM2
Other presentations are also available on the web :
- bijective programming, on Medium
- the-km2-preprocessor-for-programming-and-writing
- Toward the end of text-based programming
- Extending the python language : KM2 as a macro processor
1.1. Some quick examples
using greek characters and usual math notation :
|
This is a python program
|
1.2. Programming with KM2
1.2.1. Programming with tables
Suppose we want to write some similar functions that are very similar.We define a table as a variable :
First define the table of functions as a tree variable : |
|
The table can now appear in the text :
|
Now we can define all the functions with just a macro
and the extraction macro for python |
|
def fact (n) : if n < 1 : return 1 else : return n * fact (n-1) def fib (n) : if n < 2 : return 1 else : return fib (n-1) + fib (n-2) def sum_squares (n) : if n < 1 : return 0 else : return n*n + sum_squares (n-1) |
And also some js code :
and the extraction macro for javascript |
|
function fact (n) { if (n < 1) { return 1; } else { return n * fact (n-1); } } function fib (n) { if (n < 2) { return 1; } else { return fib (n-1) + fib (n-2); } } function sum_squares (n) { if (n < 1) { return 0; } else { return n*n + sum_squares (n-1); } } |
1.2.2. The 'WHERE' syntax
A simple example |
|
|
1.3. Writing documents with KM2
Suppose we want to write some more documentation about the functions defined aboveWe redefine a table as a variable :
Now we generate the code with the documentation
First define the table of functions as a tree variable : and the extraction macro for python |
|
|
First we improve slightly the documentation with more information, hyperlinks and graphics: Now we can also generate 'on the fly', more documentation for the code : The output is not in the same order as in the table or in the declaration Also we can see that programm sources can not embed documenttaion with graphics. |
|
#INFORMATIONS #--- fact (n) --- #tested ok #--- fib (n) --- #Here, fib is computed in fib (n) time # can be computed in log (n) time #--- sum_squares (n) --- #tested ok #NOW THE CODE |
But we can do better (cf below) !
Now, we can generate text for a better documentation :
|
|
List of functions : -- fact(n) -- fib(n) -- sum_squares(n)
|
1.4. Writing CSS with tables
Writing CSS styles is quite painful and difficult.KM2 allows to use tables which are easier to define, read and maintain.
STEP 1 : define the selectors and their values :
Example of css styles for headers
|
The values of variables can be defined as variabe in the file or by defining palettes :
|
Of course, we define several tables similarly for navbar, articles, etc
Now we describe the macros and generate the file :
palette using css variables at the beginning of the file
STEP 2 : write macros to generate the css Macros for the generation of css files Step 3 : generate the css file |
We get :
and : |
Done.
...