Skip to content

Commit

Permalink
Presenter improvements and new features
Browse files Browse the repository at this point in the history
 * Presenter now tries to write style given in event
 * Can write single quoted
 * several fixes to line length handling
 * sever other presenter fixes
 * updated server deps
 * updated server code to use new presenter features
   (or rather, not use, as the server *should* alter the
   input style instead of outputting it as-is)
  • Loading branch information
flyx committed Nov 11, 2023
1 parent a2f289a commit 03c95da
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 170 deletions.
21 changes: 19 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
## 2.1.0 (upcoming)

Features:

* The presenter now honors the node style set in the events it presents,
if possible. So if a scalar is set to be a literal block scalar, it is
presented as such unless impossible or presenter options specifically
prevent this.
* The presenter can now output single-quoted scalars. It only does so when
this scalar style is explicitly set on an event.

Changes:

* renamed ``canonicalDumper`` / ``setCanonicalStyle`` to
``explanatoryDumper`` / ``setExplanatoryStyle`` because it was
a misnomer and there is nothing canonical about this output style.
The terminology *canonical* was carried over from PyYAML, but the
YAML specification uses that term for different things.
The old names are kept with a ``deprecated`` pragma.
* The ``explanatoryDumper`` now automatically enables the
tag shorthand ``!n!``, because in this style you want that for readability.

Bugfixes:

* Fixed a bug that prevented instances of generic types to be used in ``Option``
fields (e.g. ``Option[seq[string]]``) (#101)
* Fixed a problem that caused invalid indentation when dumping with certain
* Fixed a bug that caused invalid indentation when dumping with certain
settings (#140)
* Fixed parsing errors for verbatim tags in flow style (#140)
* Fixed a problem that caused presentation of block scalars in
* Fixed a bug that caused presentation of block scalars in
flow collections (#140)
* Fixed a bug that sometimes caused the last word of a folded block scalar
not to be presented.
* Fixed maximum line length not properly implemented in presenter in a number
of cases.
* Fixed a bug that prevented the presenter from outputting compact
flow mappings in cMixed mode.
* Fixed block scalars as mapping keys not being presented properly.

## 2.0.0

Expand Down
4 changes: 2 additions & 2 deletions doc/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ updated as you type.
<label for="style-default">Default</label>
</div>
<div class="style-option">
<input type="radio" name="style" id="style-canonical" value="canonical" checked="checked"/>
<label for="style-canonical">Canonical</label>
<input type="radio" name="style" id="style-explanatory" value="explanatory" checked="checked"/>
<label for="style-explanatory">Explanatory</label>
</div>
<div class="style-option">
<input type="radio" name="style" id="style-block" value="block"/>
Expand Down
15 changes: 8 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
};
outputs = { self, nixpkgs, utils, nix-filter }:
let
version = "1.1.0";
version = "2.1.0";
systemDependent = with utils.lib;
eachSystem allSystems (system:
let pkgs = nixpkgs.legacyPackages.${system};
in with nix-filter.lib; {
devShell = pkgs.mkShell { buildInputs = with pkgs; [ nim2 ]; };
packages.webdocs = let
nim-jester = pkgs.stdenv.mkDerivation {
name = "nim-jester-0.5.0";
name = "nim-jester-0.6.0";
src = pkgs.fetchFromGitHub {
owner = "dom96";
repo = "jester";
rev = "v0.5.0";
rev = "v0.6.0";
sha256 =
"0m8a4ss4460jd2lcbqcbdd68jhcy35xg7qdyr95mh8rflwvmcvhk";
"sha256-F/zWWGipJ4lBE3njceXn5HBFTYEXB4l2rk6+FfqqZTQ=";
};
dontBuild = true;
installPhase = ''
Expand All @@ -29,17 +29,18 @@
'';
};
nim-httpbeast = pkgs.stdenv.mkDerivation {
name = "nim-httpbeast-0.2.2";
name = "nim-httpbeast-0.4.1";
src = pkgs.fetchFromGitHub {
owner = "dom96";
repo = "httpbeast";
rev = "v0.2.2";
rev = "v0.4.1";
sha256 =
"1f8ch7sd5kcyaw1b1lpqywvhx2h6aa5an37zm7x0j22giqlml5c6";
"sha256-8ncCj94UeirSevgZP717NiNtecDyH5jHky+QId31IvQ=";
};
dontBuild = true;
installPhase = ''
mkdir -p $out/lib
ls -alh
cp -r src/* $out/lib
'';
};
Expand Down
4 changes: 2 additions & 2 deletions server/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ router nyRouter:
try:
case @"style"
of "minimal": dumper.setMinimalStyle()
of "canonical": dumper.setCanonicalStyle()
of "explanatory": dumper.setExplanatoryStyle()
of "default": dumper.setDefaultStyle()
of "json": dumper.setJsonStyle()
of "block": dumper.setBlockOnlyStyle()
Expand All @@ -48,7 +48,7 @@ router nyRouter:
var
output = newStringStream()
highlighted = ""
dumper.transform(newStringStream(@"input"), output, true)
dumper.transform(newStringStream(@"input"), output, @"style" == "explanatory")

# syntax highlighting (stolen and modified from stlib's rstgen)
var g: GeneralTokenizer
Expand Down
63 changes: 62 additions & 1 deletion test/tpresenter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# See the file "copying.txt", included in this
# distribution, for details about the copyright.

import std/[ options, strutils ]
import ../yaml/[ presenter, data, stream ]
import unittest
import commonTestUtils
Expand Down Expand Up @@ -32,6 +33,15 @@ suite "Presenter":
test "Scalar without tag":
var input = inputSingle(scalarEvent("droggeljug"))
assertOutput(input, "droggeljug\n")

test "Root block scalar":
var input = inputSingle(scalarEvent("I am a dwarf and I'm digging a hole\n diggy diggy hole\n diggy diggy hole\n"))
assertOutput(input,
"--- |\n" &
"I am a dwarf and I'm digging a hole\n" &
" diggy diggy hole\n" &
" diggy diggy hole\n",
PresentationOptions(maxLineLength: some(40)))

test "Compact flow sequence":
var input = inputSingle(startSeqEvent(), scalarEvent("1"), scalarEvent("2"), endSeqEvent())
Expand Down Expand Up @@ -87,4 +97,55 @@ suite "Presenter":
var input = inputSingle(startMapEvent(), scalarEvent("root"), startSeqEvent(), scalarEvent("a"),
startMapEvent(), scalarEvent("1"), scalarEvent("2"), scalarEvent("3"), scalarEvent("4"), endMapEvent(),
endSeqEvent(), endMapEvent())
assertOutput(input, "root:\n - a\n - 1: 2\n 3: 4\n", PresentationOptions(outputVersion: ovNone, indentationStep: 4))
assertOutput(input, "root:\n - a\n - 1: 2\n 3: 4\n", PresentationOptions(outputVersion: ovNone, indentationStep: 4))

test "Scalar output with explicit style set":
var input = inputSingle(
startSeqEvent(), scalarEvent("plain", style = ssPlain),
scalarEvent("@noplain", style = ssPlain),
scalarEvent("literal\n", style = ssLiteral),
scalarEvent("nofolded ", style = ssFolded),
scalarEvent("folded scalar", style = ssFolded),
scalarEvent("single'", style = ssSingleQuoted),
endSeqEvent()
)
assertOutput(input, "- plain\n" &
"- \"@noplain\"\n" &
"- |\n" &
" literal\n" &
"- \"nofolded \"\n" &
"- >-\n" &
" folded scalar\n" &
"- 'single'''\n")

test "Collection output with explicit style set":
var input = inputSingle(
startMapEvent(), scalarEvent("a", style = ssDoubleQuoted),
startSeqEvent(style = csFlow), scalarEvent("b"), scalarEvent("c"), scalarEvent("d"), endSeqEvent(),
scalarEvent("? e"), startSeqEvent(style = csBlock), scalarEvent("f"), endSeqEvent(),
scalarEvent("g"), startMapEvent(), scalarEvent("h"), scalarEvent("i"), endMapEvent(),
scalarEvent("j"), startMapEvent(), scalarEvent("k", style = ssLiteral), scalarEvent("l"), endMapEvent(),
endMapEvent()
)
assertOutput(input, "\"a\": [b, c, d]\n" &
"\"? e\":\n" &
" - f\n" &
"g: {h: i}\n" &
"j:\n" &
" ? |-\n" &
" k\n" &
" : l\n")

test "Block mapping with multiline keys":
var input = inputSingle(
startMapEvent(), scalarEvent(repeat("a", 18)), scalarEvent("b"),
scalarEvent(repeat("\"", 8)), scalarEvent("c"),
scalarEvent(repeat("d", 17)), scalarEvent("e"), endMapEvent())
assertOutput(input, "? \"aaaaaaaaaaaaaaa\\\n" &
" aaa\"\n" &
": b\n" &
"? \"\\\"\\\"\\\"\\\"\\\"\\\"\\\"\\\n" &
" \\\"\"\n" &
": c\n" &
"ddddddddddddddddd:\n" &
" e\n", PresentationOptions(maxLineLength: some(19)))
Loading

0 comments on commit 03c95da

Please sign in to comment.