Skip to content
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
24 changes: 22 additions & 2 deletions tools/MarkdownConverter.Tests/MarkdownSourceConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using DocumentFormat.OpenXml;
using System.Xml.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Packaging;
using MarkdownConverter.Converter;
using MarkdownConverter.Spec;
using Org.XmlUnit;
using Org.XmlUnit.Builder;
using Org.XmlUnit.Diff;
using System.Xml.Linq;
using Xunit;

namespace MarkdownConverter.Tests;
Expand Down Expand Up @@ -94,6 +95,25 @@ public void LongLineWarnings(int lineLength, bool code, int expectedWarningCount
Assert.Equal(expectedWarningCount, reporter.Warnings);
}

[Fact]
public void LongLineWarnings_AcceptedList()
{
foreach (var acceptedLine in HardCodedConfig.AcceptedLongLines)
{
string text = $"# 1 Heading\r\n```csharp\r\n{acceptedLine}\r\n```\r\n";
var reporter = new Reporter(null, "test.md");
var spec = MarkdownSpec.ReadFiles(["test.md"], reporter, _ => new StringReader(text));
var resultDoc = WordprocessingDocument.Create(new MemoryStream(), WordprocessingDocumentType.Document);
var source = spec.Sources.Single();
var converter = new MarkdownSourceConverter(source.Item2, wordDocument: resultDoc,
spec: spec,
filename: "test.md",
reporter);

Assert.Equal(0, reporter.Warnings);
}
}

[Theory]
[InlineData("Valid\r\n\r\n- Item 1\r\n- Item 2", 0)]
[InlineData("Valid\r\n\r\n* Item 1\r\n* Item 2", 0)]
Expand Down
25 changes: 25 additions & 0 deletions tools/MarkdownConverter/Converter/HardCodedConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Immutable;

namespace MarkdownConverter.Converter;

/// <summary>
/// Aspects of configuration which are hard-coded for convenience, but could be extracted later.
/// </summary>
public static class HardCodedConfig
{
// Long lines that are either known to actually be okay in the Word output, or
// are acceptable and better than trying to break them. These do not issue warnings.
public static ImmutableList<string> AcceptedLongLines = new List<string>
{
// expressions.md (12.8.11 Null Conditional Invocation Expression) and grammar.md
// These genuinely wrap, but that's better than breaking the lines.
" : null_conditional_member_access null_forgiving_operator? '(' argument_list? ')'",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The only thing I might consider here is adding a comment stating which clause (in expressions.md) that these productions appear.

I agree this feels like a hack in some ways, but I like the design in that if we edit those lines, suddenly warnings appear and we need to look to see if the new content works. The only stale risk is if we shorten these lines and they remain forever as unneeded accepted long lines.

Noting where they are in the standard might entice us to remove those no longer needed while editing any that changed. (We're just detailed enough to consider it).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Have added more detail for the location in a separate commit - not just the number, but the title, so it's easier to find if we renumber.

I did wonder about checking at the end of the conversion whether every long line listed for the file that's being converted ended up being used... but it felt like overengineering in the end. Can explore that if you'd like.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree that's more than we need. Let's :shipit:

" | null_conditional_element_access null_forgiving_operator? '(' argument_list? ')'",
// expressions.md (12.8.22 Stack allocation) and grammar.md
// Again, genuinely wrapping but better than breaking lines.
" | 'stackalloc' unmanaged_type? '[' constant_expression? ']' stackalloc_initializer",
// classes.md (15.2.5 Type parameter constraints)
// This doesn't end up wrapping.
" // nonnullable base class requirement allows nonnullable class type argument",
}.ToImmutableList();
}
6 changes: 3 additions & 3 deletions tools/MarkdownConverter/Converter/MarkdownSourceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
{
// The number of null-forgiving operators here is alarming, but ultimately it's simpler to accept
// an NRE being thrown if we have a malformed document than to try to guard against it.
var numbering = wordDocument.MainDocumentPart!.NumberingDefinitionsPart!.Numbering.OfType<NumberingInstance>().First(ni => ni.NumberID!.Value == numberingId);
var numbering = wordDocument.MainDocumentPart!.NumberingDefinitionsPart!.Numbering!.OfType<NumberingInstance>().First(ni => ni.NumberID!.Value == numberingId);
var abstractNumberingId = numbering.AbstractNumId!.Val;
var abstractNumbering = wordDocument.MainDocumentPart.NumberingDefinitionsPart.Numbering.OfType<AbstractNum>().First(ani => ani.AbstractNumberId!.Value == abstractNumberingId!);
var abstractNumbering = wordDocument.MainDocumentPart.NumberingDefinitionsPart.Numbering!.OfType<AbstractNum>().First(ani => ani.AbstractNumberId!.Value == abstractNumberingId!);
foreach (var level in abstractNumbering.OfType<Level>())
{
var paragraphProperties = level.GetFirstChild<ParagraphProperties>();
Expand Down Expand Up @@ -353,7 +353,7 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
foreach (var line in lines)
{
int lineLength = line.Words.Sum(w => w.Text.Length);
if (lineLength > MaximumCodeLineLength)
if (lineLength > MaximumCodeLineLength && !HardCodedConfig.AcceptedLongLines.Contains(line.ToString()))
{
reporter.Warning(DiagnosticIDs.MDC032, $"Line length {lineLength} > maximum {MaximumCodeLineLength};\r\n{line}", lineOffset: lineOffset);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/MarkdownConverter/Converter/MarkdownSpecConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void ConvertToWord(MarkdownSpec spec, string templateFile, string
resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
}

var body = resultDoc.MainDocumentPart!.Document.Body!;
var body = resultDoc.MainDocumentPart!.Document!.Body!;

ReplaceTableOfContents(spec, body);

Expand Down