Switch to nanoc

This commit is contained in:
flaviut 2014-05-31 15:40:34 -04:00
commit 6bfe583522
24 changed files with 307 additions and 74 deletions

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
src/_book/
!*.md !*.md
nohup.out nohup.out
output/
tmp/

47
Rules Normal file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env ruby
# A few helpful tips about the Rules file:
#
# * The string given to #compile and #route are matching patterns for
# identifiers--not for paths. Therefore, you can’t match on extension.
#
# * The order of rules is important: for each item, only the first matching
# rule is applied.
#
# * Item identifiers start and end with a slash (e.g. “/about/” for the file
# “content/about.html”). To select all children, grandchildren, … of an
# item, use the pattern “/about/*/”; “/about/*” will also select the parent,
# because “*” matches zero or more characters.
compile '*' do
if item.identifier == '/toc/'
filter :kramdown
layout 'blank'
elsif item[:extension] == 'md'
filter :kramdown
filter :colorize_syntax, :default_colorizer => :pygmentsrb
layout 'default'
elsif item[:extension] == 'css'
# don’t filter stylesheets
elsif item.binary?
# don’t filter binary items
else
filter :erb
layout 'default'
end
end
route '*' do
if item[:extension] == 'css'
# Write item with identifier /foo/ to /foo.css
item.identifier.chop + '.css'
elsif item.binary?
# Write item with identifier /foo/ to /foo.ext
item.identifier.chop + '.' + item[:extension]
else
# Write item with identifier /foo/ to /foo/index.html
item.identifier + 'index.html'
end
end
layout '*', :erb

View file

@ -7,7 +7,7 @@ Nimrod also supports case statements, which are like switches in other languages
* case statements, like most things, are actually expressions * case statements, like most things, are actually expressions
* It is required that every possible case be covered * It is required that every possible case be covered
``` Nimrod ~~~ Nimrod
case "charlie" case "charlie"
of "alfa": of "alfa":
echo "A" echo "A"
@ -38,11 +38,11 @@ proc positiveOrNegative(num: int): string =
"impossible" "impossible"
echo positiveOrNegative(-1) echo positiveOrNegative(-1)
``` ~~~
``` ~~~
$ nimrod c -r ./case_stmts.nim $ nimrod c -r ./case_stmts.nim
C C
Consonant Consonant
negative negative
``` ~~~

View file

@ -6,7 +6,7 @@ Nimrod has first class iterators and syntax to use them, for loops. The `continu
When iterating over an object with one item, Nimrod will call an iterator called `items` with the first parameter the type you want to iterate over. The same thing happens when iterating with two items, but in that case, the `pairs` iterator is called. When iterating over an object with one item, Nimrod will call an iterator called `items` with the first parameter the type you want to iterate over. The same thing happens when iterating with two items, but in that case, the `pairs` iterator is called.
``` nimrod ~~~ nimrod
type type
TRange = object TRange = object
low: int low: int
@ -24,18 +24,18 @@ iterator pairs(range: TRange): tuple[a: int, b: char] =
for i, c in TRange(low: 1, high: 3): for i, c in TRange(low: 1, high: 3):
echo c echo c
``` ~~~
``` ~~~
$ nimrod c -r items_pair.nim $ nimrod c -r items_pair.nim
b b
c c
d d
``` ~~~
## Inline Iterators ## Inline Iterators
Inline iterators basically take the body of the for loop and inline it into the iterator. This means that they do not have any overhead from function calling, but if carelessly created may increase code size dramatically. Inline iterators basically take the body of the for loop and inline it into the iterator. This means that they do not have any overhead from function calling, but if carelessly created may increase code size dramatically.
``` nimrod ~~~ nimrod
iterator countTo(n: int): int = iterator countTo(n: int): int =
var i = 0 var i = 0
while i <= n: while i <= n:
@ -44,8 +44,8 @@ iterator countTo(n: int): int =
for i in countTo(5): for i in countTo(5):
echo i echo i
``` ~~~
``` ~~~
$ nimrod c -r ./inline_iter.nim $ nimrod c -r ./inline_iter.nim
0 0
1 1
@ -53,13 +53,13 @@ $ nimrod c -r ./inline_iter.nim
3 3
4 4
5 5
``` ~~~
## Closure Iterators ## Closure Iterators
Closure iterators hold on to their state and can be resumed at any time. The `finished()` function can be used to check if there are any more elements available in the iterator. Closure iterators hold on to their state and can be resumed at any time. The `finished()` function can be used to check if there are any more elements available in the iterator.
``` nimrod ~~~ nimrod
proc countTo(n: int): iterator(): int = proc countTo(n: int): iterator(): int =
return iterator (): int = return iterator (): int =
var i = 0 var i = 0
@ -81,10 +81,10 @@ let countTo9 = countTo(9)
for i in countTo9: for i in countTo9:
output.add($i) output.add($i)
echo output echo output
``` ~~~
``` ~~~
$ nimrod c -r ./closure_iter.nim $ nimrod c -r ./closure_iter.nim
0 0
12345678910111213141516171819200 12345678910111213141516171819200
0123456789 0123456789
``` ~~~

View file

@ -19,7 +19,7 @@ Once you have downloaded the appropriate distribution and extracted the files so
## Downloading from Git ## Downloading from Git
To install Nimrod from git, ensure that you have gcc installed on Windows, Linux, and BSDs and clang on Mac. Afterwards, run the following series of commands to download and bootstrap Nimrod: To install Nimrod from git, ensure that you have gcc installed on Windows, Linux, and BSDs and clang on Mac. Afterwards, run the following series of commands to download and bootstrap Nimrod:
``` ~~~
git clone -b master git://github.com/Araq/Nimrod.git git clone -b master git://github.com/Araq/Nimrod.git
cd Nimrod cd Nimrod
git clone --depth 1 git://github.com/nimrod-code/csources git clone --depth 1 git://github.com/nimrod-code/csources
@ -28,6 +28,6 @@ sh build.sh # Replace with build.bat on windows
cd .. cd ..
bin/nimrod c koch bin/nimrod c koch
./koch boot -d:release ./koch boot -d:release
``` ~~~
Once compiled, you may want to add the `bin` directory to your path for easy access. Once compiled, you may want to add the `bin` directory to your path for easy access.

View file

@ -2,16 +2,16 @@
The code for a simple hello world program is as follows: The code for a simple hello world program is as follows:
``` nimrod ~~~ Nimrod
echo "Hello World" echo "Hello World"
``` ~~~
To compile and execute the program, the following command should be run To compile and execute the program, the following command should be run
``` ~~~
$ nimrod c -r --verbosity:0 ./helloworld.nim $ nimrod c -r --verbosity:0 ./helloworld.nim
Hello World Hello World
``` ~~~
The command has several elements: The command has several elements:

View file

@ -4,7 +4,7 @@ Nimrod many different control flow constructs, including the standard `if`s, `el
When inside a loop or block, it is possible to use `continue` or `break` any point, where `continue` skips to the next iteration and `break` ends the loop. The break statement can also receive the name of the block to break out of, so it is possible to break out of nested loops. When inside a loop or block, it is possible to use `continue` or `break` any point, where `continue` skips to the next iteration and `break` ends the loop. The break statement can also receive the name of the block to break out of, so it is possible to break out of nested loops.
``` Nimrod ~~~ Nimrod
import strutils import strutils
let answer = 4 # Chosen by a fair dice roll, let answer = 4 # Chosen by a fair dice roll,
@ -25,4 +25,4 @@ block busyloops:
while true: while true:
while true: while true:
break busyloops break busyloops
``` ~~~

View file

@ -1,6 +1,6 @@
Nimrod is a powerful statically typed language that allows the programmer expressiveness without compromising run-time performance. Nimrod is a powerful statically typed language that allows the programmer expressiveness without compromising run-time performance.
``` nimrod ~~~ nimrod
import tables, strutils import tables, strutils
var wordFrequencies = initTable[string, int] var wordFrequencies = initTable[string, int]
@ -17,4 +17,4 @@ for word, frequency in wordFrequencies:
mostFrequentWord = word mostFrequentWord = word
echo "The most frequent word is '", word, "'" echo "The most frequent word is '", word, "'"
``` ~~~

View file

@ -10,13 +10,13 @@ To indicate the size of an integer literal, append `u` or `i` and the size you'd
Integers can also have `0[xX]`, `0o`, `0[Bb]` prepended to indicate a hex, octal, or binary literal, respectively. Underscores are also valid in literals, and can help with readability. Integers can also have `0[xX]`, `0o`, `0[Bb]` prepended to indicate a hex, octal, or binary literal, respectively. Underscores are also valid in literals, and can help with readability.
``` nimrod ~~~ nimrod
let let
a: int8 = 0x7F # Works a: int8 = 0x7F # Works
b: uint8 = 0x1111_1111 # Works b: uint8 = 0x1111_1111 # Works
d = 0xFF # type is int d = 0xFF # type is int
c: uint8 = 256 # Compile time error c: uint8 = 256 # Compile time error
``` ~~~
Precedence rules are the same as most other languages, but instead of `^`, `&`, `|`, `>>`, `<<`, the `xor`, `and`, `or`, `shr`, `shl` operators are used, respectively. Precedence rules are the same as most other languages, but instead of `^`, `&`, `|`, `>>`, `<<`, the `xor`, `and`, `or`, `shr`, `shl` operators are used, respectively.

View file

@ -2,7 +2,7 @@
In Nimrod, objects are like structs from C family languages and define a grouping of fields. In Nimrod, objects are like structs from C family languages and define a grouping of fields.
``` nimrod ~~~ nimrod
type type
Animal* = object Animal* = object
name*, genus*, species*: string name*, genus*, species*: string
@ -35,4 +35,4 @@ let spot = PAnimal(name: "Spot",
# Same as before, only pointer value is unchangeable # Same as before, only pointer value is unchangeable
spot.age = 2 spot.age = 2
``` ~~~

View file

@ -2,7 +2,7 @@
# OOP Macro # OOP Macro
This is the code that we currently must write to use OOP in nimrod: This is the code that we currently must write to use OOP in nimrod:
```nimrod ~~~nimrod
type Animal = type Animal =
ref object {.inheritable.} ref object {.inheritable.}
name: string name: string
@ -15,11 +15,11 @@ method vocalize(self: Dog): string = "woof"
type Cat = ref object of Animal type Cat = ref object of Animal
method vocalize(self: Cat): string = "meow" method vocalize(self: Cat): string = "meow"
``` ~~~
All these typedefs and `self: T` parameters are repetitive, so it'd be good to write a macro to mask them. Something like this would be best: All these typedefs and `self: T` parameters are repetitive, so it'd be good to write a macro to mask them. Something like this would be best:
```nimrod ~~~nimrod
class(Animal of TObject): class(Animal of TObject):
var name: string var name: string
method vocalize: string = "..." method vocalize: string = "..."
@ -38,9 +38,9 @@ proc run_test() =
pets.add Dog(name: "ruffles") pets.add Dog(name: "ruffles")
assert(@["meow", "woof"] == @[pets[0].vocalize, assert(@["meow", "woof"] == @[pets[0].vocalize,
pets[1].vocalize]) pets[1].vocalize])
``` ~~~
```nimrod ~~~nimrod
import macros import macros
macro class*(): stmt {.immediate.} = macro class*(): stmt {.immediate.} =
@ -214,4 +214,4 @@ class(Cat of Animal) do:
run_test() run_test()
echo "Tests passed!" echo "Tests passed!"
``` ~~~

101
content/stylesheet.css Normal file
View file

@ -0,0 +1,101 @@
* {
margin: 0;
padding: 0;
font-family: Georgia, Palatino, serif;
}
body {
background: #fff;
}
a {
text-decoration: none;
}
a:link,
a:visited {
color: #f30;
}
a:hover {
color: #f90;
}
#main {
position: absolute;
top: 40px;
left: 280px;
width: 500px;
}
#main h1 {
font-size: 40px;
font-weight: normal;
line-height: 40px;
letter-spacing: -1px;
}
#main p {
margin: 20px 0;
font-size: 15px;
line-height: 20px;
}
#main ul, #main ol {
margin: 20px;
}
#main li {
font-size: 15px;
line-height: 20px;
}
#main ul li {
list-style-type: square;
}
#sidebar {
position: absolute;
top: 40px;
left: 20px;
width: 200px;
padding: 20px 20px 0 0;
border-right: 1px solid #ccc;
text-align: right;
}
#sidebar h2 {
text-transform: uppercase;
font-size: 13px;
color: #333;
letter-spacing: 1px;
line-height: 20px;
}
#sidebar ul {
list-style-type: none;
margin: 20px 0;
}
#sidebar li {
font-size: 14px;
line-height: 20px;
}

16
content/toc.md Normal file
View file

@ -0,0 +1,16 @@
## Summary
* [Getting Started](getting_started/)
* [Hello World](hello_world/)
* [Numbers](numbers/)
* [Variables](variables/page/)
* [Result](variables/result/)
* [Objects](objects/)
* [Enums](enums/)
* [If, Else, While, Block](if_else_while/)
* [Case Statements](case/)
* [For Loops & Iterators](for_iterators/)
* [Exceptions](exceptions/)
* [Functions]()
* [Varargs](varargs/)
* [OOP Macro](oop_macro/)

View file

@ -2,41 +2,41 @@
Standard varargs simply allows you to pass multiple parameters to your function. Standard varargs simply allows you to pass multiple parameters to your function.
``` nimrod ~~~ nimrod
proc printThings(things: varargs[string]) = proc printThings(things: varargs[string]) =
for thing in things: for thing in things:
echo thing echo thing
printThings "words", "to", "print" printThings "words", "to", "print"
``` ~~~
``` ~~~
$ nimrod c -r ./varargs1.nim $ nimrod c -r ./varargs1.nim
words words
to to
print print
``` ~~~
However, trying to run However, trying to run
``` nimrod ~~~ nimrod
printThings 1, "string", @[1, 2, 3] printThings 1, "string", @[1, 2, 3]
``` ~~~
will fail to compile because the compiler won't coerce anything into strings. Luckily enough, there is a tool to fix this, the coercing varargs: will fail to compile because the compiler won't coerce anything into strings. Luckily enough, there is a tool to fix this, the coercing varargs:
``` nimrod ~~~ nimrod
proc printThings(things: varargs[string, `$`]) = proc printThings(things: varargs[string, `$`]) =
for thing in things: for thing in things:
echo thing echo thing
printThings "thing 1", 2, @[4, 5, 6] printThings "thing 1", 2, @[4, 5, 6]
``` ~~~
``` ~~~
$ nimrod c -r ./varargs2.nim $ nimrod c -r ./varargs2.nim
thing 1 thing 1
2 2
@[4, 5, 6] @[4, 5, 6]
``` ~~~
It works by finding a function `` `$` `` that returns string for each argument type, and applying it to each argument. It works by finding a function `` `$` `` that returns string for each argument type, and applying it to each argument.

View file

@ -2,7 +2,7 @@
Nimrod supports three different types of variables, `let`, `var`, and `const`. As with most things, multiple variables can be declared in the same section. Nimrod supports three different types of variables, `let`, `var`, and `const`. As with most things, multiple variables can be declared in the same section.
``` nimrod ~~~ nimrod
proc getAlphabet(): string = proc getAlphabet(): string =
result = "" result = ""
for letter in 'a'..'z': for letter in 'a'..'z':
@ -23,22 +23,22 @@ b.inc # This is also fine.
c.add "bar" # This will result in an error. c.add "bar" # This will result in an error.
d.inc # As will this. d.inc # As will this.
``` ~~~
``` ~~~
$ nimrod c --verbosity:2 ./assignment.nim $ nimrod c --verbosity:2 ./assignment.nim
a20.nim(19, 0) Error: for a 'var' type a variable needs to be passed a20.nim(19, 0) Error: for a 'var' type a variable needs to be passed
c.add "bar" # This will result in an error. c.add "bar" # This will result in an error.
^ ^
``` ~~~
Without `--verbosity:2` only the error will be shown. Without `--verbosity:2` only the error will be shown.
A `const` variable's value will be evaluated at compile-time, so if you inspect the C sources, you'll see the following line: A `const` variable's value will be evaluated at compile-time, so if you inspect the C sources, you'll see the following line:
``` C ~~~ C
STRING_LITERAL(TMP129, "abcdefghijklmnopqrstuvwxyz", 26); STRING_LITERAL(TMP129, "abcdefghijklmnopqrstuvwxyz", 26);
``` ~~~
The limitation with this is that procedures which are evaluated at compile-time cannot interface with C because there is no compile-time foreign function interface at this time. The limitation with this is that procedures which are evaluated at compile-time cannot interface with C because there is no compile-time foreign function interface at this time.

View file

@ -2,19 +2,19 @@
The `result` variable is a special variable that serves as an implicit return variable. This is useful because in most cases, the control flow semantics of the `return` statement are unneeded. `result` is initialized with the default value for a given return type, so the following code starts as `0`, before being initialized to `number`: The `result` variable is a special variable that serves as an implicit return variable. This is useful because in most cases, the control flow semantics of the `return` statement are unneeded. `result` is initialized with the default value for a given return type, so the following code starts as `0`, before being initialized to `number`:
``` nimrod ~~~ nimrod
proc `**`(number, power: int): int = proc `**`(number, power: int): int =
result = number result = number
for i in 1..power: for i in 1..power:
result *= number result *= number
``` ~~~
A possible gotcha is declaring a variable called result and expecting it to have the same semantics. A possible gotcha is declaring a variable called result and expecting it to have the same semantics.
``` nimrod ~~~ nimrod
proc unexpected(): int = proc unexpected(): int =
var result = 5 var result = 5
result += 5 result += 5
echo unexpected() # Prints 0, not 10 echo unexpected() # Prints 0, not 10
``` ~~~

1
layouts/blank.html Normal file
View file

@ -0,0 +1 @@
<%= yield %>

19
layouts/default.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A Brand New nanoc Site - <%= @item[:title] %></title>
<link rel="stylesheet" href="<%= @items['/stylesheet/'].path %>">
<!-- you don't need to keep this, but it's cool for stats! -->
<meta name="generator" content="nanoc <%= Nanoc::VERSION %>">
</head>
<body>
<div id="main">
<%= yield %>
</div>
<div id="sidebar">
<%= @items["/toc/"].compiled_content(:snapshot => :last) %>
</div>
</body>
</html>

2
lib/default.rb Normal file
View file

@ -0,0 +1,2 @@
# All files in the 'lib' directory will be loaded
# before nanoc starts compiling.

62
nanoc.yaml Normal file
View file

@ -0,0 +1,62 @@
# A list of file extensions that nanoc will consider to be textual rather than
# binary. If an item with an extension not in this list is found, the file
# will be considered as binary.
text_extensions: [ 'coffee', 'css', 'erb', 'haml', 'handlebars', 'hb', 'htm', 'html', 'js', 'less', 'markdown', 'md', 'ms', 'mustache', 'php', 'rb', 'sass', 'scss', 'txt', 'xhtml', 'xml' ]
# The path to the directory where all generated files will be written to. This
# can be an absolute path starting with a slash, but it can also be path
# relative to the site directory.
output_dir: output
# A list of index filenames, i.e. names of files that will be served by a web
# server when a directory is requested. Usually, index files are named
# “index.html”, but depending on the web server, this may be something else,
# such as “default.htm”. This list is used by nanoc to generate pretty URLs.
index_filenames: [ 'index.html' ]
# Whether or not to generate a diff of the compiled content when compiling a
# site. The diff will contain the differences between the compiled content
# before and after the last site compilation.
enable_output_diff: false
prune:
# Whether to automatically remove files not managed by nanoc from the output
# directory. For safety reasons, this is turned off by default.
auto_prune: false
# Which files and directories you want to exclude from pruning. If you version
# your output directory, you should probably exclude VCS directories such as
# .git, .svn etc.
exclude: [ '.git', '.hg', '.svn', 'CVS' ]
# The data sources where nanoc loads its data from. This is an array of
# hashes; each array element represents a single data source. By default,
# there is only a single data source that reads data from the “content/” and
# “layout/” directories in the site directory.
data_sources:
-
# The type is the identifier of the data source. By default, this will be
# `filesystem_unified`.
type: filesystem_unified
# The path where items should be mounted (comparable to mount points in
# Unix-like systems). This is “/” by default, meaning that items will have
# “/” prefixed to their identifiers. If the items root were “/en/”
# instead, an item at content/about.html would have an identifier of
# “/en/about/” instead of just “/about/”.
items_root: /
# The path where layouts should be mounted. The layouts root behaves the
# same as the items root, but applies to layouts rather than items.
layouts_root: /
# Whether to allow periods in identifiers. When turned off, everything
# past the first period is considered to be the extension, and when
# turned on, only the characters past the last period are considered to
# be the extension. For example, a file named “content/about.html.erb”
# will have the identifier “/about/” when turned off, but when turned on
# it will become “/about.html/” instead.
allow_periods_in_identifiers: false
# The default encoding for all files in `content/` and `layouts/`.
encoding: utf-8

View file

@ -1,16 +0,0 @@
# Summary
* [Getting Started](getting_started.md)
* [Hello World](hello_world.md)
* [Numbers](numbers.md)
* [Variables](variables/page.md)
* [Result](variables/result.md)
* [Objects](objects.md)
* [Enums](enums.md)
* [If, Else, While, Block](if_else_while.md)
* [Case Statements](case.md)
* [For Loops & Iterators](for_iterators.md)
* [Exceptions](exceptions.md)
* [Functions]()
* [Varargs](varargs.md)
* [OOP Macro](oop_macro.md)