Skip to content

Trimming of template values #19

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion MwParserFromScratch/MwParserUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ public static string NormalizeTemplateArgumentName(string argumentName)
return argumentName.Trim();
}

/// <summary>
/// Normalizes and manipulates a template argument name or value.
/// </summary>
/// <inheritdoc cref="NormalizeTemplateArgumentText(Wikitext)"/>
/// <param name="text">The wikitext to be manipulated.</param>
internal static void NormalizeTemplateArgumentText(Wikitext text)
{
if (text.Lines.First() is Paragraph firstParagraph)
{
if (firstParagraph.Inlines.First() is PlainText firstPlainText)
firstPlainText.Content = firstPlainText.Content.TrimStart();
}

if (text.Lines.Last() is Paragraph lastParagraph)
{
if (lastParagraph.Inlines.Last() is PlainText lastPlainText)
lastPlainText.Content = lastPlainText.Content.TrimEnd();
}
}

/// <inheritdoc cref="NormalizeImageLinkArgumentName(string)"/>
/// <param name="argumentName">The argument name to be normalized. The node will be converted into its string representation.</param>
public static string NormalizeImageLinkArgumentName(Node argumentName)
Expand Down Expand Up @@ -80,7 +100,7 @@ public static string NormalizeTitle(Node title)
public static string NormalizeTitle(string title)
{
if (string.IsNullOrEmpty(title)) return title;
var parts = title.Split(new[] {':'}, 2);
var parts = title.Split(new[] { ':' }, 2);
for (int i = 0; i < parts.Length; i++)
parts[i] = Utility.NormalizeTitlePart(parts[i], false);
return string.Join(":", parts);
Expand Down
5 changes: 4 additions & 1 deletion MwParserFromScratch/ParserCore.Expandable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ private TemplateArgument ParseTemplateArgument()
CurrentContext.Terminator = null;
var value = ParseWikitext();
Debug.Assert(value != null);

MwParserUtility.NormalizeTemplateArgumentText(value);

return ParseSuccessful(new TemplateArgument(a, value));
}
return ParseSuccessful(new TemplateArgument(null, a));
Expand Down Expand Up @@ -287,7 +290,7 @@ private bool ParseUntilClosingTag(TagNode tag)
return true;
}
closingTagMatch = matcher.Match(closingTag);
CLOSE_TAG:
CLOSE_TAG:
Debug.Assert(closingTagMatch.Success);
Debug.Assert(closingTagMatch.Groups[1].Success);
Debug.Assert(closingTagMatch.Groups[2].Success);
Expand Down