Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedent multiline text to preserve content indentation #197

Merged
merged 9 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/mappers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const join = list =>
.filter(value => value !== Symbol.for("eof"))
.join("");

// Prune unmatched maybes from a list.
export const prune = list =>
list.filter(value => value !== null);

// Map a list of {name, value} aliases into an array of values.
export const keep_abstract = list =>
list
Expand Down
43 changes: 34 additions & 9 deletions syntax/abstract.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function list_into(Type) {
case FTL.Pattern:
return elements =>
always(new FTL.Pattern(
elements
dedent(elements)
.reduce(join_adjacent(FTL.TextElement), [])
.map(trim_text_at_extremes)
.filter(remove_empty_text)));
Expand Down Expand Up @@ -111,33 +111,38 @@ export function into(Type) {
}
}

// Create a reducer suitable for joining adjacent nodes of the same type, if
// type is one of types specified.
function join_adjacent(...types) {
return function(acc, cur) {
let prev = acc[acc.length - 1];
for (let Type of types) {
if (prev instanceof Type && cur instanceof Type) {
join_of_type(Type, prev, cur);
// Replace prev with a new node of the same type whose value is
// the sum of prev and cur, and discard cur.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... discard cur and prev.?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's needed given that the comment starts with Replace...?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could go without the , and discard ... altogether, too.

acc[acc.length - 1] = join_of_type(Type, prev, cur);
return acc;
}
}
return acc.concat(cur);
};
}

// Join values of two or more nodes of the same type. Return a new node.
function join_of_type(Type, ...elements) {
// TODO Join annotations and spans.
switch (Type) {
case FTL.TextElement:
return elements.reduce((a, b) =>
(a.value += b.value, a));
new Type(a.value + b.value));
case FTL.Comment:
case FTL.GroupComment:
case FTL.ResourceComment:
return elements.reduce((a, b) =>
(a.content += `\n${b.content}`, a));
new Type(a.content + "\n" + b.content));
case FTL.Junk:
return elements.reduce((a, b) =>
(a.content += b.content, a));
new Type(a.content + b.content));
}
}

Expand All @@ -154,18 +159,38 @@ function attach_comments(acc, cur) {
}
}

const LEADING_BLANK = /^[ \n\r]*/;
const TRAILING_BLANK = /[ \n\r]*$/;
// Remove the largest common indentation from a list of elements of a Pattern.
// The indents are parsed in grammar.mjs and passed to abstract.mjs as string
// primitives along with other PatternElements.
function dedent(elements) {
// Calculate the maximum common indent.
let indents = elements.filter(element => typeof element === "string");
let common = Math.min(...indents.map(indent => indent.length));

function trim_indents(element) {
if (typeof element === "string") {
// Trim the indent and convert it to a proper TextElement.
// It will be joined with its adjacents later on.
return new FTL.TextElement(element.slice(common));
}
return element;
}

return elements.map(trim_indents);
}

const LEADING_BLANK_BLOCK = /^\n*/;
const TRAILING_BLANK_INLINE = / *$/;

function trim_text_at_extremes(element, index, array) {
if (element instanceof FTL.TextElement) {
if (index === 0) {
element.value = element.value.replace(
LEADING_BLANK, "");
LEADING_BLANK_BLOCK, "");
}
if (index === array.length - 1) {
element.value = element.value.replace(
TRAILING_BLANK, "");
TRAILING_BLANK_INLINE, "");
}
}
return element;
Expand Down
19 changes: 9 additions & 10 deletions syntax/grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
regex, repeat, repeat1, sequence, string
} from "../lib/combinators.mjs";
import {
element_at, flatten, join, keep_abstract, mutate, print
element_at, flatten, join, keep_abstract, mutate, print, prune
} from "../lib/mappers.mjs";

/* ----------------------------------------------------- */
Expand Down Expand Up @@ -151,12 +151,11 @@ let inline_text = defer(() =>

let block_text = defer(() =>
sequence(
blank_block.chain(into(FTL.TextElement)).abstract,
blank_block.chain(into(FTL.TextElement)),
blank_inline,
indented_char.chain(into(FTL.TextElement)).abstract,
maybe(inline_text.abstract)
)
.map(keep_abstract));
indented_char.chain(into(FTL.TextElement)),
maybe(inline_text))
.map(prune));

let inline_placeable = defer(() =>
sequence(
Expand All @@ -173,10 +172,10 @@ let inline_placeable = defer(() =>

let block_placeable = defer(() =>
sequence(
blank_block.chain(into(FTL.TextElement)).abstract,
maybe(blank_inline),
inline_placeable.abstract)
.map(keep_abstract));
blank_block.chain(into(FTL.TextElement)),
// No indent before a placeable counts as 0 in dedention logic.
maybe(blank_inline).map(s => s || ""),
inline_placeable));

/* ------------------------------------------------------------------- */
/* Rules for validating expressions in Placeables and as selectors of
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/multiline_values.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,28 @@ key07 =
{"A multiline value"} starting and ending {"with a placeable"}

key08 = Leading and trailing whitespace.

key09 = zero
three
two
one
zero

key10 =
two
zero
four

key11 =


two
zero

key12 =
{"."}
four

key13 =
four
{"."}
106 changes: 105 additions & 1 deletion test/fixtures/multiline_values.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"elements": [
{
"type": "TextElement",
"value": "A multiline value with non-standard\n\nindentation."
"value": "A multiline value with non-standard\n\n indentation."
}
]
},
Expand Down Expand Up @@ -212,6 +212,110 @@
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key09"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": "zero\n three\n two\n one\nzero"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key10"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": " two\nzero\n four"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key11"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": " two\nzero"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key12"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "Placeable",
"expression": {
"type": "StringLiteral",
"value": "."
}
},
{
"type": "TextElement",
"value": "\n four"
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "key13"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "TextElement",
"value": " four\n"
},
{
"type": "Placeable",
"expression": {
"type": "StringLiteral",
"value": "."
}
}
]
},
"attributes": [],
"comment": null
}
]
}