diff --git a/.gitignore b/.gitignore index 5091696..e38d486 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -src/_book/ !*.md nohup.out +output/ +tmp/ diff --git a/Rules b/Rules new file mode 100644 index 0000000..cf6e573 --- /dev/null +++ b/Rules @@ -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 diff --git a/src/book.json b/content/book.json similarity index 100% rename from src/book.json rename to content/book.json diff --git a/src/case.md b/content/case.md similarity index 97% rename from src/case.md rename to content/case.md index b2fb5a5..27df99d 100644 --- a/src/case.md +++ b/content/case.md @@ -7,7 +7,7 @@ Nimrod also supports case statements, which are like switches in other languages * case statements, like most things, are actually expressions * It is required that every possible case be covered -``` Nimrod +~~~ Nimrod case "charlie" of "alfa": echo "A" @@ -38,11 +38,11 @@ proc positiveOrNegative(num: int): string = "impossible" echo positiveOrNegative(-1) -``` +~~~ -``` +~~~ $ nimrod c -r ./case_stmts.nim C Consonant negative -``` \ No newline at end of file +~~~ \ No newline at end of file diff --git a/src/enums.md b/content/enums.md similarity index 100% rename from src/enums.md rename to content/enums.md diff --git a/src/exceptions.md b/content/exceptions.md similarity index 100% rename from src/exceptions.md rename to content/exceptions.md diff --git a/src/for_iterators.md b/content/for_iterators.md similarity index 96% rename from src/for_iterators.md rename to content/for_iterators.md index 6e9e122..088f171 100644 --- a/src/for_iterators.md +++ b/content/for_iterators.md @@ -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. -``` nimrod +~~~ nimrod type TRange = object low: int @@ -24,18 +24,18 @@ iterator pairs(range: TRange): tuple[a: int, b: char] = for i, c in TRange(low: 1, high: 3): echo c -``` -``` +~~~ +~~~ $ nimrod c -r items_pair.nim b c d -``` +~~~ ## 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. -``` nimrod +~~~ nimrod iterator countTo(n: int): int = var i = 0 while i <= n: @@ -44,8 +44,8 @@ iterator countTo(n: int): int = for i in countTo(5): echo i -``` -``` +~~~ +~~~ $ nimrod c -r ./inline_iter.nim 0 1 @@ -53,13 +53,13 @@ $ nimrod c -r ./inline_iter.nim 3 4 5 -``` +~~~ ## 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. -``` nimrod +~~~ nimrod proc countTo(n: int): iterator(): int = return iterator (): int = var i = 0 @@ -81,10 +81,10 @@ let countTo9 = countTo(9) for i in countTo9: output.add($i) echo output -``` -``` +~~~ +~~~ $ nimrod c -r ./closure_iter.nim 0 12345678910111213141516171819200 0123456789 -``` \ No newline at end of file +~~~ \ No newline at end of file diff --git a/src/getting_started.md b/content/getting_started.md similarity index 99% rename from src/getting_started.md rename to content/getting_started.md index fcd9a37..5403dfc 100644 --- a/src/getting_started.md +++ b/content/getting_started.md @@ -19,7 +19,7 @@ Once you have downloaded the appropriate distribution and extracted the files so ## 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: -``` +~~~ git clone -b master git://github.com/Araq/Nimrod.git cd Nimrod git clone --depth 1 git://github.com/nimrod-code/csources @@ -28,6 +28,6 @@ sh build.sh # Replace with build.bat on windows cd .. bin/nimrod c koch ./koch boot -d:release -``` +~~~ Once compiled, you may want to add the `bin` directory to your path for easy access. \ No newline at end of file diff --git a/src/hello_world.md b/content/hello_world.md similarity index 96% rename from src/hello_world.md rename to content/hello_world.md index 576fbae..7d6bdb3 100644 --- a/src/hello_world.md +++ b/content/hello_world.md @@ -2,20 +2,20 @@ The code for a simple hello world program is as follows: -``` nimrod +~~~ Nimrod echo "Hello World" -``` +~~~ To compile and execute the program, the following command should be run -``` +~~~ $ nimrod c -r --verbosity:0 ./helloworld.nim Hello World -``` +~~~ The command has several elements: * `c` is an alias for `compile`, which compiles the Nimrod sources into C and then invokes the C compiler on them * `-r` is an alias for `--run`, which runs the program * `--verbosity:0` makes the compiler only output essential messages, since by default it also outputs some debugging messages. From now on, we assume that `--verbosity:0` is set -* `./helloworld.nim` is the path to the source you want to compile \ No newline at end of file +* `./helloworld.nim` is the path to the source you want to compile diff --git a/src/if_else_while.md b/content/if_else_while.md similarity index 98% rename from src/if_else_while.md rename to content/if_else_while.md index e94e321..6e3bed4 100644 --- a/src/if_else_while.md +++ b/content/if_else_while.md @@ -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. -``` Nimrod +~~~ Nimrod import strutils let answer = 4 # Chosen by a fair dice roll, @@ -25,4 +25,4 @@ block busyloops: while true: while true: break busyloops -``` \ No newline at end of file +~~~ \ No newline at end of file diff --git a/src/README.md b/content/index.md similarity index 97% rename from src/README.md rename to content/index.md index f6ecfd9..a0ed5f9 100644 --- a/src/README.md +++ b/content/index.md @@ -1,6 +1,6 @@ Nimrod is a powerful statically typed language that allows the programmer expressiveness without compromising run-time performance. -``` nimrod +~~~ nimrod import tables, strutils var wordFrequencies = initTable[string, int] @@ -17,4 +17,4 @@ for word, frequency in wordFrequencies: mostFrequentWord = word echo "The most frequent word is '", word, "'" -``` \ No newline at end of file +~~~ \ No newline at end of file diff --git a/src/numbers.md b/content/numbers.md similarity index 98% rename from src/numbers.md rename to content/numbers.md index bd36131..6e77dc7 100644 --- a/src/numbers.md +++ b/content/numbers.md @@ -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. -``` nimrod +~~~ nimrod let a: int8 = 0x7F # Works b: uint8 = 0x1111_1111 # Works d = 0xFF # type is int 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. diff --git a/src/objects.md b/content/objects.md similarity index 98% rename from src/objects.md rename to content/objects.md index 4d93e83..699947a 100644 --- a/src/objects.md +++ b/content/objects.md @@ -2,7 +2,7 @@ In Nimrod, objects are like structs from C family languages and define a grouping of fields. -``` nimrod +~~~ nimrod type Animal* = object name*, genus*, species*: string @@ -35,4 +35,4 @@ let spot = PAnimal(name: "Spot", # Same as before, only pointer value is unchangeable spot.age = 2 -``` +~~~ diff --git a/src/oop_macro.md b/content/oop_macro.md similarity index 99% rename from src/oop_macro.md rename to content/oop_macro.md index bc7b311..fa97214 100644 --- a/src/oop_macro.md +++ b/content/oop_macro.md @@ -2,7 +2,7 @@ # OOP Macro This is the code that we currently must write to use OOP in nimrod: -```nimrod +~~~nimrod type Animal = ref object {.inheritable.} name: string @@ -15,11 +15,11 @@ method vocalize(self: Dog): string = "woof" type Cat = ref object of Animal 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: -```nimrod +~~~nimrod class(Animal of TObject): var name: string method vocalize: string = "..." @@ -38,9 +38,9 @@ proc run_test() = pets.add Dog(name: "ruffles") assert(@["meow", "woof"] == @[pets[0].vocalize, pets[1].vocalize]) -``` +~~~ -```nimrod +~~~nimrod import macros macro class*(): stmt {.immediate.} = @@ -214,4 +214,4 @@ class(Cat of Animal) do: run_test() echo "Tests passed!" -``` \ No newline at end of file +~~~ \ No newline at end of file diff --git a/content/stylesheet.css b/content/stylesheet.css new file mode 100644 index 0000000..7d8c8ac --- /dev/null +++ b/content/stylesheet.css @@ -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; +} diff --git a/content/toc.md b/content/toc.md new file mode 100644 index 0000000..bfb2df8 --- /dev/null +++ b/content/toc.md @@ -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/) diff --git a/src/varargs.md b/content/varargs.md similarity index 92% rename from src/varargs.md rename to content/varargs.md index 0e0a40e..69cddbc 100644 --- a/src/varargs.md +++ b/content/varargs.md @@ -2,41 +2,41 @@ Standard varargs simply allows you to pass multiple parameters to your function. -``` nimrod +~~~ nimrod proc printThings(things: varargs[string]) = for thing in things: echo thing printThings "words", "to", "print" -``` -``` +~~~ +~~~ $ nimrod c -r ./varargs1.nim words to print -``` +~~~ However, trying to run -``` nimrod +~~~ nimrod 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: -``` nimrod +~~~ nimrod proc printThings(things: varargs[string, `$`]) = for thing in things: echo thing printThings "thing 1", 2, @[4, 5, 6] -``` +~~~ -``` +~~~ $ nimrod c -r ./varargs2.nim thing 1 2 @[4, 5, 6] -``` +~~~ It works by finding a function `` `$` `` that returns string for each argument type, and applying it to each argument. \ No newline at end of file diff --git a/src/variables/page.md b/content/variables/page.md similarity index 97% rename from src/variables/page.md rename to content/variables/page.md index d6c3274..d3ba06d 100644 --- a/src/variables/page.md +++ b/content/variables/page.md @@ -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 +~~~ nimrod proc getAlphabet(): string = result = "" for letter in 'a'..'z': @@ -23,22 +23,22 @@ b.inc # This is also fine. c.add "bar" # This will result in an error. d.inc # As will this. -``` +~~~ -``` +~~~ $ nimrod c --verbosity:2 ./assignment.nim a20.nim(19, 0) Error: for a 'var' type a variable needs to be passed c.add "bar" # This will result in an error. ^ -``` +~~~ 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: -``` C +~~~ C 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. diff --git a/src/variables/result.md b/content/variables/result.md similarity index 95% rename from src/variables/result.md rename to content/variables/result.md index 82b950d..c3f9fb0 100644 --- a/src/variables/result.md +++ b/content/variables/result.md @@ -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`: -``` nimrod +~~~ nimrod proc `**`(number, power: int): int = result = number for i in 1..power: result *= number -``` +~~~ A possible gotcha is declaring a variable called result and expecting it to have the same semantics. -``` nimrod +~~~ nimrod proc unexpected(): int = var result = 5 result += 5 echo unexpected() # Prints 0, not 10 -``` +~~~ diff --git a/layouts/blank.html b/layouts/blank.html new file mode 100644 index 0000000..37f0bdd --- /dev/null +++ b/layouts/blank.html @@ -0,0 +1 @@ +<%= yield %> diff --git a/layouts/default.html b/layouts/default.html new file mode 100644 index 0000000..f62af5e --- /dev/null +++ b/layouts/default.html @@ -0,0 +1,19 @@ + + + + + A Brand New nanoc Site - <%= @item[:title] %> + + + + + + +
+ <%= yield %> +
+ + + diff --git a/lib/default.rb b/lib/default.rb new file mode 100644 index 0000000..a4df0cc --- /dev/null +++ b/lib/default.rb @@ -0,0 +1,2 @@ +# All files in the 'lib' directory will be loaded +# before nanoc starts compiling. diff --git a/nanoc.yaml b/nanoc.yaml new file mode 100644 index 0000000..bf8c595 --- /dev/null +++ b/nanoc.yaml @@ -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 diff --git a/src/SUMMARY.md b/src/SUMMARY.md deleted file mode 100644 index 321a0de..0000000 --- a/src/SUMMARY.md +++ /dev/null @@ -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) \ No newline at end of file