PHP implementation of the Links Notation (lino) parser and formatter.
composer require link-foundation/links-notationOr add the dependency to your composer.json:
{
"require": {
"link-foundation/links-notation": "^0.19"
}
}For contributors working on the source code:
cd php
composer installRun tests:
composer run-script testCheck the coding standard (PSR-12):
composer run-script lintFix what can be fixed automatically:
composer run-script lint:fix<?php
require __DIR__ . '/vendor/autoload.php';
use LinkFoundation\LinksNotation\Parser;
$parser = new Parser();
$input = <<<'LINO'
papa (lovesMama: loves mama)
son lovesMama
daughter lovesMama
all (love mama)
LINO;
foreach ($parser->parse($input) as $link) {
echo $link, PHP_EOL;
}use LinkFoundation\LinksNotation\Link;
$parent = new Link('parent', [new Link('child1'), new Link('child2')]);
echo $parent; // (parent: child1 child2)
echo $parent->id; // parent
echo count($parent->values); // 2
echo $parent->format(true); // parent: child1 child2use LinkFoundation\LinksNotation\Formatter;
use LinkFoundation\LinksNotation\Parser;
$parser = new Parser();
$links = $parser->parse("papa lovesMama\nson lovesMama");
echo Formatter::formatLinks($links); // (papa lovesMama)\n(son lovesMama)
echo Formatter::formatLinks($links, true); // papa lovesMama\nson lovesMamause LinkFoundation\LinksNotation\FormatConfig;
use LinkFoundation\LinksNotation\Link;
$link = new Link('id', [new Link('1'), new Link('2'), new Link('3'), new Link('4')]);
$config = new FormatConfig(maxInlineRefs: 3, preferInline: false);
echo $link->format($config);
// id:
// 1
// 2
// 3
// 4use LinkFoundation\LinksNotation\Formatter;
use LinkFoundation\LinksNotation\Parser;
$parser = new Parser();
$input = <<<'LINO'
parent
child1
child2
grandchild
LINO;
echo Formatter::formatLinks($parser->parse($input));papa (lovesMama: loves mama)
son lovesMama
daughter lovesMama
all (love mama)
papa has car
mama has house
(papa and mama) are happy
(linksNotation: links notation)
(This is a linksNotation as well)
(linksNotation supports (unlimited number (of references) in each link))
parent
child1
child2
grandchild1
grandchild2
3:
papa
loves
mama
This is equivalent to:
(3: papa loves mama)
A parenthesized group opens a nested context: its body starts fresh at indentation level zero and follows the same rules as the root document, so a line break inside parentheses is structure rather than decoration.
value (
id "1"
label "one"
)
The document above parses to (value ((id 1) (label one))) - two children, each
a link of its own - rather than to one flat list in which the boundary between
id and label would be lost. A body that stays on a single line still
collapses to a single link, so (a b c) is unchanged.
$input = <<<'LINO'
value (
id "1"
label "one"
)
LINO;
echo Formatter::formatLinks($parser->parse($input)); // (value ((id 1) (label one)))A # hides the rest of the line it stands on, so a document can carry prose
about itself:
# the machines this deploys to
deploy: staging # only staging, for now
Both comments are gone by the time the document is read, leaving the single
link (deploy: staging). A # only opens a comment where a reference could
begin, so a # inside a token (issue#1047) and a # inside a delimited
reference ("#") stay ordinary characters.
A formatter keeps the same rule from the other side: a reference that begins
with a # is written quoted ('#tag'), so a document it writes reads back as
itself.
Comments are on by default, and a parser can be told to read # as an ordinary
character again, for documents written before comments existed:
$document = "# the machines this deploys to\ndeploy: staging # only staging, for now\n";
echo Formatter::formatLinks((new Parser())->parse($document)); // (deploy: staging)
$plain = new Parser(10 * 1024 * 1024, 1000, false);
echo Formatter::formatLinks($plain->parse("# a b\n")); // (# a b)Any number of identical quote characters (', " or `) opens a string,
and the same number closes it. Doubling the opening sequence inside the string
escapes it.
("simple" 'simple' `simple`)
(""text with " inside"")
(```const x = 1;```)
Main parser class for converting strings into links.
__construct(int $maxInputSize = 10485760, int $maxDepth = 1000, bool $comments = true)- create a parser with optional limits, and with#comments on unless$commentsisfalseparse(string $input): Link[]- parse a lino string and return the links- throws
InvalidArgumentExceptionwhen the input exceeds$maxInputSize - throws
LinkFoundation\LinksNotation\ParseExceptionwhen the input cannot be parsed
- throws
Represents a single link with an id and values.
__construct(?string $id = null, ?Link[] $values = null)- create a linkpublic ?string $id- link identifierpublic Link[] $values- child values/links__toString(): string- convert the link to its string formformat(bool|FormatConfig $lessParentheses = false, bool $isCompoundValue = false): string- format the linkequals(mixed $other): bool- structural equality with another linksimplify(): Link- unwrap a link that only holds a single valuecombine(Link $other): Link- combine two links into a single onegetValuesString(): string- format the values without the surrounding linktoLinkOrIdString(): string- format as an id when the link is a bare referencestatic escapeReference(?string $reference): string- quote a reference when needed
static formatLinks(Link[] $links, bool|FormatConfig $lessParentheses = false): string- format a document
Formatting options.
lessParentheses- omit the outer parentheses (defaultfalse)maxLineLength- line length that triggers indentation (default80)indentLongLines- enable the line length rule (defaultfalse)maxInlineRefs- number of inline references that triggers indentation (defaultnull)groupConsecutive- merge consecutive links with the same id (defaultfalse)indentString- string used for one indentation level (default two spaces)preferInline- keep links inline whenever possible (defaulttrue)shouldIndentByLength(string $line): boolshouldIndentByRefCount(int $refCount): bool
Exception thrown when parsing fails.
src/Link.php- link data structuresrc/Formatter.php- document formattingsrc/FormatConfig.php- formatting optionssrc/Parser.php- parser implementationsrc/ParseException.php- parse exceptiontests/- PHPUnit test suite
- PHP 8.4 or higher
ext-mbstring- Composer 2
- Package:
link-foundation/links-notation - Namespace:
LinkFoundation\LinksNotation - License: Unlicense (see LICENSE)