Skip to content

Commit

Permalink
Merge pull request #44 from MiguelDomingues/keep-literals-together
Browse files Browse the repository at this point in the history
Add --keep-literals-together flag
  • Loading branch information
MiguelDomingues authored Oct 3, 2023
2 parents 0cbafb9 + ccb23b5 commit 581e3f6
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,8 @@ If used, and output file exists, then the .pot file is updated.

Ignore missing .po file.
This should be used with `--keep-source-strings`.

### --keep-literals-together

When multiple literals (e.g. hyperlink, text, emphasis) are part of the same markdown block (i.e. paragraph) keep them as a single string.
This results in a string containing markdown.
4 changes: 4 additions & 0 deletions src/MarkdownLocalize.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public static int Main(string[] args)
[Option("--ignore-missing-po", "Write output if .po file is missing.", CommandOptionType.NoValue)]
public bool IgnoreMissingPO { get; } = false;

[Option("--keep-literals-together", "Keep multiple literals within the same block as a single string.", CommandOptionType.NoValue)]
public bool KeepLiteralsTogether { get; } = false;

private int OnExecute()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Expand Down Expand Up @@ -281,6 +284,7 @@ private void InitMarkdownParserOptions()
FrontMatterSourceKey = FrontMatterSourceKey,
UpdateFrontMatterLocale = UpdateFrontMatterLocale,
AddFrontMatterKeys = ParseFrontMatterKeys(),
KeepLiteralsTogether = KeepLiteralsTogether,
EnableDefinitionLists = EnableDefinitionLists,
});
}
Expand Down
1 change: 1 addition & 0 deletions src/MarkdownLocalize.Markdown/RendererOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public class RendererOptions
public bool UpdateFrontMatterLocale { get; set; }
public string Locale { get; set; }
public Dictionary<string, string> AddFrontMatterKeys { get; set; }
public bool KeepLiteralsTogether = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ c is AutolinkInline
// Move cursor to start of the obj element
renderer.MoveTo(obj.Span.Start);

if (renderer.Options.KeepLiteralsTogether)
ProcessChildsTogether(renderer, obj);
else
ProcessChildsSeparate(renderer, obj);

ExtractLabelsFromLinkInline(renderer, obj);

//if (!obj.LastChild.Span.IsEmpty)
// renderer.MoveTo(obj.LastChild.Span.End + 1);
}

private void ProcessChildsSeparate(TransformRenderer renderer, ContainerInline obj)
{
// Let's skip childs from start
IEnumerable<Inline> childs = obj;
while (childs.Count() > 0 && SkipChild(childs.First()))
Expand Down Expand Up @@ -107,6 +120,78 @@ c is AutolinkInline
ProcessChild(renderer, i);
}
}
}

private bool SkipChildTogether(Inline i)
{
switch (i)
{
case LineBreakInline:
case LinkInline l when l.IsImage:
case LiteralInline li when li.Content.ToString().Trim() == "":
return true;
}
return false;
}

private void ProcessChildsTogether(TransformRenderer renderer, ContainerInline obj)
{
// Let's skip childs from start
IEnumerable<Inline> childs = obj;
while (childs.Count() > 0 && SkipChildTogether(childs.First()))
{
ProcessChild(renderer, childs.First());
childs = childs.Skip(1);
}

if (childs.Count() > 0)
{
IEnumerable<Inline> childsEnd = new List<Inline>();

// Let's skip childs from end
while (childs.Count() > 0 && SkipChildTogether(childs.Last()))
{
childsEnd = childsEnd.Append(childs.Last());
childs = childs.SkipLast(1);
}
// Childs are in reverse order
childsEnd = childsEnd.Reverse().ToList();

if (childs.Count() == 1)
{
if (SkipChild(childs.First()))
{
ProcessChild(renderer, childs.First());
childs = childs.Skip(1);
}
else
{
switch (childs.First())
{
case EmphasisInline ei: // If only emphasis, then use childs of emphasis
childs = ei;
break;
case CodeInline: // If only code inline, ignore
case AutolinkInline: // if only auto link, ignore
childs = childs.Skip(1);
break;
}
}
}
if (childs.Count() > 0)
{
renderer.WriteMultipleTogether(childs, renderer.LastWrittenIndex);
}

foreach (Inline i in childsEnd)
{
ProcessChild(renderer, i);
}
}
}

private static void ExtractLabelsFromLinkInline(TransformRenderer renderer, ContainerInline obj)
{
// Extract labels from urls
IEnumerable<LinkInline> links = obj.Where(c => c is LinkInline).Cast<LinkInline>().Where(l => l.FirstChild != null);
foreach (LinkInline l in links)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ private void WriteRaw(string raw, int index)
}
}

private void WriteMultipleTogether(IEnumerable<Inline> childs, int index)
{
int startIndex = childs.Where(c => c.Span.Start > 0).First().Span.Start;
int endIndex = childs.Where(c => c.Span.End > 0).Last().Span.End;
string str = OriginalMarkdown.Substring(startIndex, endIndex - startIndex + 1);

int trimStartIndex = str.Length - str.TrimStart().Length;
string trimStart = str.Substring(0, trimStartIndex);
int trimEndIndex = str.TrimEnd().Length;
string trimEnd = str.Substring(trimEndIndex);

string transformedS = CheckTransform(str.Trim(), index, true);

if (transformedS == null)
throw new Exception("Missing translation for: " + str);

Write(trimStart);
Write(transformedS);
Write(trimEnd);

SkipTo(endIndex + 1);
}

private void WriteMultiple(IEnumerable<Inline> childs, int index)
{
// Let's trim all lines, while saving trimmed text
Expand Down
81 changes: 81 additions & 0 deletions test/MarkdownLocalize.Tests/MarkdownExtractStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ public void CodeBlock()
[Fact]
public void CodeBlockSurroundedByText()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
});
IEnumerable<string> strings = MarkdownParser.ExtractStrings(@"See the following example:
```language
a = b
Expand Down Expand Up @@ -568,5 +571,83 @@ And a last line
IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "Term 1", "This is a definition item", "And a last line", "This ia another definition item" }, strings);
}
[Fact]
public void MultipleLiteralsSeparate()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
KeepLiteralsTogether = false,
});
string md = @" [Label](https://www.example.com) and text. ";

IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "Label", "and text." }, strings);
}


[Fact]
public void MultipleLiteralsTogether()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
KeepLiteralsTogether = true,
});
string md = @" [Label](https://www.example.com) and text. ";

IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "[Label](https://www.example.com) and text.", "Label" }, strings);
}

[Fact]
public void MultipleLiteralsTogetherImage()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
KeepLiteralsTogether = true,
});
string md = @"![](image.png) Text ";

IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "Text" }, strings);
}

[Fact]
public void MultipleLiteralsTogetherBold()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
KeepLiteralsTogether = true,
});
string md = @"**Bold text**";

IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "Bold text" }, strings);
}

[Fact]
public void MultipleLiteralsTogetherBullet()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
KeepLiteralsTogether = true,
});
string md = @"* ![](image.png) Text ";

IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "Text" }, strings);
}

[Fact]
public void MultipleLiteralsTogetherBulletLink()
{
MarkdownParser.SetParserOptions(new RendererOptions()
{
KeepLiteralsTogether = true,
});
string md = @"* [Label](www.example.com)";

IEnumerable<string> strings = MarkdownParser.ExtractStrings(md, null).Select(si => si.String).Distinct();
Assert.Equal(new string[] { "Label" }, strings);
}

}

0 comments on commit 581e3f6

Please sign in to comment.