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

Add status checks to the word converter #1136

Merged
merged 7 commits into from
Jun 27, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/renumber-sections.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
env:
DOTNET_NOLOGO: true
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
BillWagner marked this conversation as resolved.
Show resolved Hide resolved

steps:
- name: Check out our repo
Expand All @@ -32,4 +33,4 @@ jobs:
- name: Run section renumbering dry run
run: |
cd tools
./run-section-renumber.sh ${{ github.event.pull_request.head.sha }}
./run-section-renumber.sh
3 changes: 2 additions & 1 deletion .github/workflows/update-on-merge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
pull-requests: write
env:
DOTNET_NOLOGO: true
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- name: Check out our repo
Expand All @@ -50,7 +51,7 @@ jobs:
id: renumber-sections
run: |
cd tools
./run-section-renumber-update.sh
./run-section-renumber.sh
shell: bash

- name: Create pull request
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/word-converter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ on:
jobs:
word-converter:
runs-on: ubuntu-latest
permissions:
checks: write
env:
DOTNET_NOLOGO: true

GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}

steps:
- name: Check out our repo
uses: actions/checkout@v2
Expand Down
6 changes: 3 additions & 3 deletions tools/MarkdownConverter.Tests/MarkdownSourceConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MarkdownSourceConverterTests
[InlineData("table-with-emphasis")]
public void SingleResourceConversion(string name, bool includeNumbering = false)
{
var reporter = new Reporter(TextWriter.Null);
var reporter = new Reporter(null, $"{name}.md");
var expectedXml = ReadResource($"{name}.xml");
var spec = MarkdownSpec.ReadFiles(new[] { $"{name}.md" }, reporter, name => new StreamReader(new MemoryStream(ReadResource(name))));

Expand Down Expand Up @@ -82,7 +82,7 @@ public void LongLineWarnings(int lineLength, bool code, int expectedWarningCount
string line = new string('x', lineLength);
string suffix = code ? "```\r\n" : "";
string text = $"# 1 Heading\r\n{prefix}{line}\r\n{suffix}";
var reporter = new Reporter(TextWriter.Null);
var reporter = new Reporter(null, "test.md");
var spec = MarkdownSpec.ReadFiles(new[] { "test.md" }, reporter, _ => new StringReader(text));
var resultDoc = WordprocessingDocument.Create(new MemoryStream(), WordprocessingDocumentType.Document);
var source = spec.Sources.Single();
Expand All @@ -105,7 +105,7 @@ public void LongLineWarnings(int lineLength, bool code, int expectedWarningCount
public void InvalidListStartErrors(string text, int expectedErrorCount)
{
text = $"# 1 Heading\r\n{text}";
var reporter = new Reporter(TextWriter.Null);
var reporter = new Reporter();
var spec = MarkdownSpec.ReadFiles(new[] { "test.md" }, reporter, _ => new StringReader(text));
Assert.Equal(expectedErrorCount, reporter.Errors);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/MarkdownConverter.Tests/MarkdownSpecFileListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class MarkdownSpecFileListTests
[Fact]
public void EmptyListTest()
{
Assert.Throws<ArgumentNullException>(() => MarkdownSpec.ReadFiles(null!, new Reporter(TextWriter.Null)));
Assert.Throws<ArgumentNullException>(() => MarkdownSpec.ReadFiles(null!, new Reporter()));
}
}
34 changes: 34 additions & 0 deletions tools/MarkdownConverter/Converter/DiagnosticIDs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace MarkdownConverter.Converter;

internal class DiagnosticIDs
{
public const string MDC002 = nameof(MDC002);
BillWagner marked this conversation as resolved.
Show resolved Hide resolved
public const string MDC003 = nameof(MDC003);

public const string MDC008 = nameof(MDC008);
public const string MDC009 = nameof(MDC009);
public const string MDC010 = nameof(MDC010);
public const string MDC011 = nameof(MDC011);
public const string MDC012 = nameof(MDC012);
public const string MDC013 = nameof(MDC013);
public const string MDC014 = nameof(MDC014);
public const string MDC015 = nameof(MDC015);
public const string MDC016 = nameof(MDC016);
public const string MDC016b = nameof(MDC016b);
public const string MDC017 = nameof(MDC017);
public const string MDC018 = nameof(MDC018);
public const string MDC019 = nameof(MDC019);
public const string MDC020 = nameof(MDC020);

public const string MDC026 = nameof(MDC026);
public const string MDC027 = nameof(MDC027);
public const string MDC028 = nameof(MDC028);
public const string MDC029 = nameof(MDC029);
public const string MDC030 = nameof(MDC030);
public const string MDC031 = nameof(MDC031);
public const string MDC032 = nameof(MDC032);
public const string MDC033 = nameof(MDC033);

// Just for status messages:
public const string MDC999 = nameof(MDC999);
}
40 changes: 21 additions & 19 deletions tools/MarkdownConverter/Converter/MarkdownSourceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)

var i = sr.Url.IndexOf("#");
string currentSection = $"{sr.Url.Substring(0, i)} {new string('#', level)} {sr.Title} [{sr.Number}]";
reporter.Log(currentSection);
reporter.Log(DiagnosticIDs.MDC999, currentSection);
yield break;
}

Expand Down Expand Up @@ -170,12 +170,12 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
}
else
{
reporter.Error("MD31", "Table in quoted block does not start with table properties");
reporter.Error(DiagnosticIDs.MDC031, "Table in quoted block does not start with table properties");
}
}
else
{
reporter.Error("MD30", $"Unhandled element type in quoted block: {element.GetType()}");
reporter.Error(DiagnosticIDs.MDC030, $"Unhandled element type in quoted block: {element.GetType()}");
}
}
yield break;
Expand Down Expand Up @@ -310,7 +310,7 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
}
else
{
reporter.Error("MD08", $"Unexpected item in list '{content.GetType().Name}'");
reporter.Error(DiagnosticIDs.MDC008, $"Unexpected item in list '{content.GetType().Name}'");
}
}
}
Expand Down Expand Up @@ -344,17 +344,18 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
lines = Colorize.PlainText(code);
break;
default:
reporter.Error("MD09", $"unrecognized language {lang}");
reporter.Error(DiagnosticIDs.MDC009, $"unrecognized language {lang}");
lines = Colorize.PlainText(code);
break;
}

int lineOffset = 0;
foreach (var line in lines)
{
int lineLength = line.Words.Sum(w => w.Text.Length);
if (lineLength > MaximumCodeLineLength)
{
reporter.Warning("MD32", $"Line length {lineLength} > maximum {MaximumCodeLineLength}");
reporter.Warning(DiagnosticIDs.MDC032, $"Line length {lineLength} > maximum {MaximumCodeLineLength}", lineOffset: lineOffset);
}

if (onFirstLine)
Expand Down Expand Up @@ -388,6 +389,7 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
run.Append(new Text(word.Text) { Space = SpaceProcessingModeValues.Preserve });
runs.Add(run);
}
lineOffset++;
}
var p = new Paragraph() { ParagraphProperties = new ParagraphProperties(new ParagraphStyleId { Val = "Code" }) };
p.Append(runs);
Expand All @@ -402,7 +404,7 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
var table = TableHelpers.CreateTable();
if (header == null)
{
reporter.Error("MD10", "Github requires all tables to have header rows");
reporter.Error(DiagnosticIDs.MDC010, "Github requires all tables to have header rows");
}
else if (!header.Any(cell => cell.Length > 0))
{
Expand Down Expand Up @@ -495,7 +497,7 @@ IEnumerable<OpenXmlCompositeElement> Paragraph2Paragraphs(MarkdownParagraph md)
}
else
{
reporter.Error("MD11", $"Unrecognized markdown element {md.GetType().Name}");
reporter.Error(DiagnosticIDs.MDC011, $"Unrecognized markdown element {md.GetType().Name}");
yield return new Paragraph(new Run(new Text($"[{md.GetType().Name}]")));
}
}
Expand All @@ -518,13 +520,13 @@ IEnumerable<FlatItem> FlattenList(MarkdownParagraph.ListBlock md)
var content = item.Paragraph;
if (isOrdered.ContainsKey(level) && isOrdered[level] != isItemOrdered)
{
reporter.Error("MD12", "List can't mix ordered and unordered items at same level");
reporter.Error(DiagnosticIDs.MDC012, "List can't mix ordered and unordered items at same level");
}

isOrdered[level] = isItemOrdered;
if (level > 3)
{
reporter.Error("MD13", "Can't have more than 4 levels in a list");
reporter.Error(DiagnosticIDs.MDC013, "Can't have more than 4 levels in a list");
}
}
return flat;
Expand Down Expand Up @@ -610,7 +612,7 @@ IEnumerable<FlatItem> FlattenList(MarkdownParagraph.ListBlock md, int level)
}
else
{
reporter.Error("MD14", $"nothing fancy allowed in lists - specifically not '{mdp.GetType().Name}'");
reporter.Error(DiagnosticIDs.MDC014, $"nothing fancy allowed in lists - specifically not '{mdp.GetType().Name}'");
}
}
}
Expand Down Expand Up @@ -695,8 +697,8 @@ IEnumerable<OpenXmlElement> Span2Elements(MarkdownSpan md, bool nestedSpan = fal
if (context.Terms.ContainsKey(literal))
{
var def = context.Terms[literal];
reporter.Warning("MD16", $"Term '{literal}' defined a second time");
reporter.Warning("MD16b", $"Here was the previous definition of term '{literal}'", def.Loc);
reporter.Warning(DiagnosticIDs.MDC016, $"Term '{literal}' defined a second time");
reporter.Warning(DiagnosticIDs.MDC016b, $"Here was the previous definition of term '{literal}'", def.Loc);
}
else
{
Expand All @@ -710,7 +712,7 @@ IEnumerable<OpenXmlElement> Span2Elements(MarkdownSpan md, bool nestedSpan = fal
// either to emphasis some human-text or to refer to an ANTLR-production
if (!nestedSpan && md.IsEmphasis && (spans.Count() != 1 || !spans.First().IsLiteral))
{
reporter.Error("MD17", $"something odd inside emphasis");
reporter.Error(DiagnosticIDs.MDC017, $"something odd inside emphasis");
}

if (!nestedSpan && md.IsEmphasis && spans.Count() == 1 && spans.First() is MarkdownSpan.Literal spanLiteral)
Expand Down Expand Up @@ -834,7 +836,7 @@ Run CreateRun((string text, VerticalPositionValues position) part)
}
else
{
reporter.Error("MD18", $"Link anchor must be Literal or InlineCode, not '{md.GetType().Name}'");
reporter.Error(DiagnosticIDs.MDC018, $"Link anchor must be Literal or InlineCode, not '{md.GetType().Name}'");
yield break;
}

Expand All @@ -848,7 +850,7 @@ Run CreateRun((string text, VerticalPositionValues position) part)
var expectedAnchor = "§" + section.Number;
if (anchor != expectedAnchor)
{
reporter.Warning("MD19", $"Mismatch: link anchor is '{anchor}', should be '{expectedAnchor}'");
reporter.Warning(DiagnosticIDs.MDC019, $"Mismatch: link anchor is '{anchor}', should be '{expectedAnchor}'");
}
}

Expand Down Expand Up @@ -877,7 +879,7 @@ Run CreateRun((string text, VerticalPositionValues position) part)
// TODO: Make this report an error unconditionally once the subscript "latex-like" Markdown is removed.
if (url != "")
{
reporter.Error("MD28", $"Hyperlink url '{url}' unrecognized - not a recognized heading, and not http");
reporter.Error(DiagnosticIDs.MDC028, $"Hyperlink url '{url}' unrecognized - not a recognized heading, and not http");
}
}
}
Expand All @@ -889,7 +891,7 @@ Run CreateRun((string text, VerticalPositionValues position) part)

else
{
reporter.Error("MD20", $"Unrecognized markdown element {md.GetType().Name}");
reporter.Error(DiagnosticIDs.MDC020, $"Unrecognized markdown element {md.GetType().Name}");
yield return new Run(new Text($"[{md.GetType().Name}]"));
}
}
Expand Down Expand Up @@ -996,7 +998,7 @@ private static int BugWorkaroundIndent(ref MarkdownParagraph mdp, int level)

private IEnumerable<OpenXmlCompositeElement> HandleInvalidCustomBlock(string customBlockId)
{
reporter.Error("MD29", $"Invalid custom block ID: {customBlockId}");
reporter.Error(DiagnosticIDs.MDC029, $"Invalid custom block ID: {customBlockId}");
yield return new Paragraph(new Run(new Text($"Custom block {customBlockId}")));
}

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 @@ -32,7 +32,7 @@ public static void ConvertToWord(MarkdownSpec spec, string templateFile, string
wordDocument: resultDoc,
spec: spec,
filename: fileName,
reporter.WithFileName(fileName));
reporter.WithFileName(src.Item1));
BillWagner marked this conversation as resolved.
Show resolved Hide resolved
foreach (var p in converter.Paragraphs)
{
body.AppendChild(p);
Expand Down
4 changes: 4 additions & 0 deletions tools/MarkdownConverter/MarkdownConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Utilities\Utilities.csproj" />
</ItemGroup>

</Project>
16 changes: 12 additions & 4 deletions tools/MarkdownConverter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ namespace MarkdownConverter;

static class Program
{
static int Main(string[] args)
static async Task<int> Main(string[] args)
{
// mdspec2docx *.md csharp.g4 template.docx -o spec.docx
var ifiles = new List<string>();
var ofiles = new List<string>();
var head_sha = Environment.GetEnvironmentVariable("HEAD_SHA");
var token = Environment.GetEnvironmentVariable("GH_TOKEN");
string argserror = "";
for (int i = 0; i < args.Length; i++)
{
Expand Down Expand Up @@ -111,7 +113,7 @@ static int Main(string[] args)

Console.WriteLine("Reading markdown files");

var reporter = new Reporter(Console.Error);
var reporter = new Reporter();

// Read input file. If it contains a load of linked filenames, then read them instead.
var md = MarkdownSpec.ReadFiles(imdfiles, reporter);
Expand All @@ -122,7 +124,7 @@ static int Main(string[] args)
var odocfile2 = odocfile;
if (odocfile2 != odocfile)
{
reporter.Error("MD26", $"File '{odocfile}' was in use");
reporter.Error(DiagnosticIDs.MDC026, $"File '{odocfile}' was in use");
}

Console.WriteLine($"Writing '{Path.GetFileName(odocfile2)}'");
Expand All @@ -132,7 +134,7 @@ static int Main(string[] args)
}
catch (Exception ex)
{
reporter.Error("MD27", ex.ToString());
reporter.Error(DiagnosticIDs.MDC027, ex.ToString());
return 1;
}
if (odocfile2 != odocfile)
Expand All @@ -142,6 +144,12 @@ static int Main(string[] args)
}
Console.WriteLine($"Errors: {reporter.Errors}");
Console.WriteLine($"Warnings: {reporter.Warnings}");
if ((head_sha is not null) &&
(token is not null))
{
await reporter.WriteCheckStatus(token, head_sha);

}
return reporter.Errors == 0 ? 0 : 1;
}
}
9 changes: 0 additions & 9 deletions tools/MarkdownConverter/Properties/launchSettings.json

This file was deleted.

6 changes: 3 additions & 3 deletions tools/MarkdownConverter/Spec/MarkdownSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private MarkdownSpec(IEnumerable<Tuple<string, MarkdownDocument>> sources, Repor
var sr = Context.CreateSectionRef(heading, filename);
if (Sections.Any(s => s.Url == sr.Url))
{
fileReporter.Error("MD02", $"Duplicate section title {sr.Url}");
fileReporter.Error(DiagnosticIDs.MDC002, $"Duplicate section title {sr.Url}");
}
else
{
Expand All @@ -48,7 +48,7 @@ private MarkdownSpec(IEnumerable<Tuple<string, MarkdownDocument>> sources, Repor
}
catch (Exception ex)
{
fileReporter.Error("MD03", ex.Message); // constructor of SectionRef might throw
fileReporter.Error(DiagnosticIDs.MDC003, ex.Message); // constructor of SectionRef might throw
}
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ private static void ValidateLists(string file, string text, Reporter reporter)
var range = new MarkdownRange(i + 1, 1, i + 1, 1);
var span = MarkdownSpan.NewLiteral("", range);
var paragraph = MarkdownParagraph.NewSpan(FSharpList<MarkdownSpan>.Cons(span, FSharpList<MarkdownSpan>.Empty), range);
reporter.Error("MD33", "Invalid start of list: needs a blank line.", new SourceLocation(file, null, paragraph, null));
reporter.Error(DiagnosticIDs.MDC033, "Invalid start of list: needs a blank line.", new SourceLocation(file, null, paragraph, null));
}
}
}
Expand Down
Loading
Loading