Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Aug 21, 2023
1 parent d7fd803 commit b3ae9a1
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 219 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: marggers
name: margrave

on:
push:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# marggers
# margrave

Tweakable parser for a dialect of markdown that generates an HTML-like
representation, which can be converted to HTML. Not stable.
[Try in browser](https://metagn.github.io/marggers/browser/converter.html).
[Docs](https://metagn.github.io/marggers/docs/marggers.html)
[Try in browser](https://metagn.github.io/margrave/browser/converter.html).
[Docs](https://metagn.github.io/margrave/docs/margrave.html)

Tested for C, JS and NimScript (so also VM).

Expand Down
6 changes: 3 additions & 3 deletions browser/converter.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<meta http-equiv="Content-Language" content="en-us">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="browser.css">
<title>marggers converter</title>
<!--<link rel="shortcut icon" type="image/png" href="marggers.png">-->
<title>margrave converter</title>
<!--<link rel="shortcut icon" type="image/png" href="margrave.png">-->
</head>
<body>
<h2>marggers converter</h2>
<h2>margrave converter</h2>
<div id="head">
<textarea id="input" rows="15"></textarea>
<a href="reference.html">Reference</a>
Expand Down
12 changes: 6 additions & 6 deletions browser/converter.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ../src/[marggers, marggers/common, marggers/element], dom
import ../src/[margrave, margrave/common, margrave/element], dom

proc convertMarggers(text: NativeString): NativeString =
proc convertMargrave(text: NativeString): NativeString =
result = ""
for elem in parseMarggers(text):
for elem in parseMargrave(text):
result.add(toNativeString(elem))
result.add(NativeString("\n"))

Expand All @@ -11,12 +11,12 @@ proc input(): cstring =

proc convert() {.exportc.} =
let input = input()
let output = convertMarggers(input)
let output = convertMargrave(input)
getElementById("output").innerHTML = output

proc convertToSource() {.exportc.} =
let input = input()
let output = convertMarggers(input)
let output = convertMargrave(input)
let sourceBlock = document.createElement("pre")
sourceBlock.textContent = output
let outputElement = getElementById("output")
Expand All @@ -29,7 +29,7 @@ proc filename(): cstring =

proc download() {.exportc.} =
let input = input()
let output = convertMarggers(input)
let output = convertMargrave(input)

proc blobHtml(str: cstring): Blob {.importjs: "new Blob([#], {type: 'text/html'})", constructor.}
proc createUrl(blob: Blob): cstring {.importc: "window.URL.createObjectURL".}
Expand Down
4 changes: 2 additions & 2 deletions browser/reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta http-equiv="Content-Language" content="en-us">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="browser.css">
<title>Marggers Reference</title>
<title>Margrave Reference</title>
<style>
.column {
float: left;
Expand All @@ -17,7 +17,7 @@
clear: both;
}
</style>
<!--<link rel="shortcut icon" type="image/png" href="marggers.png">-->
<!--<link rel="shortcut icon" type="image/png" href="margrave.png">-->
</head>
<body>
<p>this page is a bit broken right now</p>
Expand Down
4 changes: 2 additions & 2 deletions marggers.nimble → margrave.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.3.3"
version = "0.4.0"
author = "metagn"
description = "markdown dialect"
license = "MIT"
Expand All @@ -18,7 +18,7 @@ task tools, "builds tools, only browser converter for now":

task docs, "build docs for all modules":
when declared(buildDocs):
buildDocs(gitUrl = "https://github.com/metagn/marggers", extraOptions = "--path:src")
buildDocs(gitUrl = "https://github.com/metagn/margrave", extraOptions = "--path:src")
else:
echo "docs task not implemented, need nimbleutils"

Expand Down
67 changes: 0 additions & 67 deletions src/marggers.nim

This file was deleted.

67 changes: 67 additions & 0 deletions src/margrave.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
## Dialect of Markdown.
##
## Example
## =======
##
## .. include:: ../examples/ref.mrg
## :literal:
##
## turns into HTML:
##
## .. include:: ../examples/ref.html
## :literal:
##
## Inline HTML note
## ****************
##
## **Note**: Nim's XML parser used for inline HTML uses `StringStream` from
## the `streams` module which does not work in JS for Nim version 1.2.x and
## earlier. To work around this, you can disable use of the XML parser,
## therefore disable inline HTML, by passing `-d:margraveNoInlineHtml`.
## This switch is available on all backends. You can still embed HTML inside
## curly braces.

import margrave/[common, element, parser, parser/defs]

export MargraveElement, element.`$`, defs

proc parseMargrave*(parser: var MargraveParser,
staticOptions: static MargraveOptions = defaultParserOptions): seq[MargraveElement] =
## Parses margrave with an already initialized parser.
result = parseTopLevel(parser, staticOptions)

proc parseMargrave*(parser: ref MargraveParser,
staticOptions: static MargraveOptions = defaultParserOptions): seq[MargraveElement] =
## Parses margrave with a reference to an already initialized parser.
result = parseTopLevel(parser[], staticOptions)

proc parseMargrave*(text: sink NativeString,
options: MargraveOptions = defaultParserOptions,
staticOptions: static MargraveOptions = defaultParserOptions): seq[MargraveElement] =
## Parses a string of text in margrave and translates it to HTML line by line.
## Result is a sequence of MargraveElements, to simply generate HTML with no need for readability
## turn these all into strings with ``$`` and join them with "".
var parser = initMargraveParser(text)
parser.options = options
result = parseMargrave(parser, staticOptions)

proc parseMargrave*(text: sink (string | cstring),
options: MargraveOptions = defaultParserOptions,
staticOptions: static MargraveOptions = defaultParserOptions): seq[MargraveElement] =
## Alias of parseMargrave that takes any string as the argument.
result = parseMargrave(NativeString(text), options, staticOptions)

proc parseMargrave*(text: sink openarray[char],
options: MargraveOptions = defaultParserOptions,
staticOptions: static MargraveOptions = defaultParserOptions): seq[MargraveElement] =
## Alias of parseMargrave that takes openarray[char] as the argument.
##
## Currently copies.
result = parseMargrave(NativeString($text), options, staticOptions)

when isMainModule:
import os, strutils
case paramStr(1)
of "parse": echo parseMargrave(paramStr(2)).join("\n")
of "file": echo parseMargrave(readFile(paramStr(2))).join("\n")
else: echo "unknown command: ", paramStr(1)
12 changes: 6 additions & 6 deletions src/marggers/common.nim → src/margrave/common.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const
marggersNoDefaultHtmlHandler* {.booldefine.} = false
margraveNoDefaultHtmlHandler* {.booldefine.} = false
## Define this to disable inline HTML at compile time completely,
## to circumvent the standard library XML parser dependency.
## This is overriden by `MarggersParser.inlineHtmlHandler`.
marggersCurlyNoHtmlEscape* {.booldefine.} = false
## The default compile time value of `MarggersOptions.curlyNoHtmlEscape`.
marggersSingleLineStaticBool* {.booldefine.} = false
## This is overriden by `MargraveParser.inlineHtmlHandler`.
margraveCurlyNoHtmlEscape* {.booldefine.} = false
## The default compile time value of `MargraveOptions.curlyNoHtmlEscape`.
margraveSingleLineStaticBool* {.booldefine.} = false
## Possible minor optimization. Not guaranteed to be faster.
marggersDelimedUseSubstrs* {.booldefine.} = false
margraveDelimedUseSubstrs* {.booldefine.} = false
## Possible minor optimization. Not guaranteed to be faster.

when defined(js) and not defined(nimdoc):
Expand Down
52 changes: 26 additions & 26 deletions src/marggers/element.nim → src/margrave/element.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type
otherTag
#text

MarggersElement* {.acyclic.} = ref object
MargraveElement* {.acyclic.} = ref object
## An individual node.
##
## Can be text, or an HTML element.
Expand All @@ -35,7 +35,7 @@ type
## with the `emptyTag` attribute.
attrs*: OrderedTable[NativeString, NativeString]
## Attributes of an HTML element.
content*: seq[MarggersElement]
content*: seq[MargraveElement]
## Inner HTML elements of an HTML element.

const EmptyTags* = {noTag, br, img, input, source, otherTag}
Expand All @@ -51,31 +51,31 @@ else:
## Returns true if `tag` is an empty tag, i.e. it has no ending tag.
tag in EmptyTags

func newStr*(s: NativeString): MarggersElement =
func newStr*(s: NativeString): MargraveElement =
## Creates a new text node with text `s`.
MarggersElement(isText: true, str: s)
MargraveElement(isText: true, str: s)

func newElem*(tag: KnownTags, content: seq[MarggersElement] = @[]): MarggersElement =
func newElem*(tag: KnownTags, content: seq[MargraveElement] = @[]): MargraveElement =
## Creates a new element node with tag `tag` and content nodes `content`.
MarggersElement(isText: false, tag: tag, content: content)
MargraveElement(isText: false, tag: tag, content: content)

func paragraphIfText*(elem: MarggersElement): MarggersElement =
func paragraphIfText*(elem: MargraveElement): MargraveElement =
## If `elem` is a text node, turns it into a <p> element.
## Otherwise returns `elem`.
if elem.isText:
MarggersElement(isText: false, tag: p, content: @[elem])
MargraveElement(isText: false, tag: p, content: @[elem])
else:
elem

proc attr*(elem: MarggersElement, key: NativeString): NativeString =
proc attr*(elem: MargraveElement, key: NativeString): NativeString =
## Gets attribute of element
elem.attrs[key]

proc attr*(elem: MarggersElement, key, val: NativeString) =
proc attr*(elem: MargraveElement, key, val: NativeString) =
## Adds attribute to element
elem.attrs[key] = val

proc attrEscaped*(elem: MarggersElement, key, val: NativeString) =
proc attrEscaped*(elem: MargraveElement, key, val: NativeString) =
## Adds attribute to element escaped
var esc =
when NativeString is string:
Expand All @@ -87,48 +87,48 @@ proc attrEscaped*(elem: MarggersElement, key, val: NativeString) =
else: esc.add v
elem.attr(key, esc)

proc hasAttr*(elem: MarggersElement, key: NativeString): bool =
proc hasAttr*(elem: MargraveElement, key: NativeString): bool =
## Checks if element has attribute
elem.attrs.hasKey(key)

proc delAttr*(elem: MarggersElement, key: NativeString) =
proc delAttr*(elem: MargraveElement, key: NativeString) =
## Deletes attribute of element
elem.attrs.del(key)

proc style*(elem: MarggersElement, style: NativeString) =
proc style*(elem: MargraveElement, style: NativeString) =
## Adds style to element
elem.attr("style", style)

func `[]`*(elem: MarggersElement, i: int): MarggersElement =
func `[]`*(elem: MargraveElement, i: int): MargraveElement =
## Indexes `elem.content`.
elem.content[i]

func `[]`*(elem: MarggersElement, i: BackwardsIndex): MarggersElement =
func `[]`*(elem: MargraveElement, i: BackwardsIndex): MargraveElement =
## Indexes `elem.content`.
elem.content[i]

func `[]=`*(elem: MarggersElement, i: int, el: MarggersElement) =
func `[]=`*(elem: MargraveElement, i: int, el: MargraveElement) =
## Indexes `elem.content`.
elem.content[i] = el

func `[]=`*(elem: MarggersElement, i: BackwardsIndex, el: MarggersElement) =
func `[]=`*(elem: MargraveElement, i: BackwardsIndex, el: MargraveElement) =
## Indexes `elem.content`.
elem.content[i] = el

func add*(elem, cont: MarggersElement) =
func add*(elem, cont: MargraveElement) =
## Adds to `elem.content`.
# was previously template, this broke vM
elem.content.add(cont)

func add*(elem: MarggersElement, cont: seq[MarggersElement]) =
func add*(elem: MargraveElement, cont: seq[MargraveElement]) =
## Appends nodes to `elem.content`.
elem.content.add(cont)

func add*(elem: MarggersElement, str: NativeString) =
func add*(elem: MargraveElement, str: NativeString) =
## Adds a text node to `elem.content`.
elem.content.add(newStr(str))

func toNativeString*(elem: MarggersElement): NativeString =
func toNativeString*(elem: MargraveElement): NativeString =
if elem.isText:
result = elem.str
else:
Expand Down Expand Up @@ -169,10 +169,10 @@ func toNativeString*(elem: MarggersElement): NativeString =
result.add(tag)
result.add('>')

func `$`*(elem: MarggersElement): string =
## Outputs a marggers element as HTML.
func `$`*(elem: MargraveElement): string =
## Outputs a margrave element as HTML.
$toNativeString(elem)

func toCstring*(elem: MarggersElement): cstring =
## Outputs a marggers element as HTML as a cstring, mostly for JS.
func toCstring*(elem: MargraveElement): cstring =
## Outputs a margrave element as HTML as a cstring, mostly for JS.
cstring(toNativeString(elem))
Loading

0 comments on commit b3ae9a1

Please sign in to comment.