Packages

A Elixir library to work with AsciiDoctor. For now it only supports reading attributes and converting to html/pdf by invoking the asciidoctor related commands.

Current section

Files

Jump to
asciidoctor lib asciidoctor.ex
Raw

lib/asciidoctor.ex

defmodule Asciidoctor do
@moduledoc """
Asciidoctor utilities
Work In Progress!
:toc: right|left
:imagesdir: ./images
:iconsdir: ./icons
:stylesdir: ./styles
:doctitle: = :title:
:description:
:keywords:
"""
def compile(file) do
file
|> File.read!()
|> parse_document
#|> extract(post)
end
def parse_document(data) do
[meta, asciidoc] = String.split(data, ~r/\n{2,}\n/, parts: 2)
parse_asciidoc(parse_meta(meta), asciidoc)
end
#Parsing
def parse_asciidoc(metadata, asciidoc) do
String.split(asciidoc, "\n")
|> parse_block(metadata)
end
def parse_block(block, metadata) do
block
#|> substitute_variables(metadata)
#|> parse_individual_block(metadata)
end
def parse_meta(meta) do
split_metadata = String.split(meta,"\n")
{title_split, remaining_meta} = split_metadata |> Enum.split(1)
title_data = parse_meta_title(title_split)
metadata = if(title_data, do: remaining_meta , else: split_metadata )
metadata
# |> Enum.map(&(parse_meta_item(&1)))
|> Enum.reduce(%{}, &(reduce_meta_attribute(&1, &2)))
|> Map.merge(%{"doctitle" => title_data, "title" => title_data})
end
def reduce_meta_attribute(%{"attribute" => attribute, "value" => ""}, acc ) do
Map.put(acc, attribute, true)
end
def reduce_meta_attribute(%{"attribute" => attribute, "value" => value}, acc ) do
Map.put(acc, attribute, value)
end
def reduce_meta_attribute(nil, acc) do
acc
end
def parse_meta_title(meta) do
Regex.named_captures(~r/^=\s*(?<title>[^\n:]+)(:[ \t](?<subtitle>[^\n]+))?/, meta)
end
#TODO unset attributes :!fba:
def parse_meta_attribute(meta) do
Regex.named_captures(~r/^:(?<attribute>[A-Za-z0-9_]{1}[A-Za-z0-9_\s-]*):([ \t]*(?<value>[^\n]+))?/, meta)
end
@doc """
Author extraction is not complete refer to using :author: instead
TODO
"""
def parse_meta_author(meta) do
#firstname middlename lastname email author_initials
# split second author by ;
Regex.named_captures(~r/^(?<author>[\w ]+) ?(<(?<email>[^\s]+)>)?/, meta)
Regex.named_captures(~r/^(?<author>[^\n]+) (<(?<url>[^\s]+)>)?/, meta)
Regex.named_captures(~r/^(?<firstname>[^\s]+)(?<middlename>[^\s]* )?(?<lastname>[^\s]* )? ?(<(?<url>[^\s]+)>)?/, meta)
end
@doc """
Revisions
v1.0, October 2, 2013: First incarnation
:revnumber:
:revdate:
:revmark:
TODO
"""
def parse_meta_revisions(meta) do
end
#replace {varname} with varname attribute!
def parse_block_section(meta) do
section = Regex.named_captures(~r/^(?<section_type>=+|#+)\s*(?<section>[^\n:]+)?/, meta)
level = if String.contains(section["section_type"], "=") do
String.count(section["section_type"]) -1
else
String.count(section["section_type"])
end
# %{"type" => "header", "header" => header, "level" => level }
%{"type" => "header", "data" => %{ "text" => section, "level" => level}}
end
@doc """
Hello world.
## Examples
iex> Asciidoctor.hello()
:world
"""
def hello do
:world
end
end