fix: Skip certain lines in guide parser

- Lines starting with whitespace
- Admonition lines
pull/5/head
Robert Dailey 3 years ago
parent fc9d51f77f
commit 4fef062895

@ -7,10 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
### Changed
- A warning is now logged when we find a number in brackets (such as `[100]`) without the word
`score` before it. This represents a potential score and bug in the guide itself.
- Release Profile Guide Parser:
- Skip lines with leading whitespace (i.e. indented lines).
- Skip admonition lines (lines starting with `!!!` or `???`).
## [1.0.0] - 2021-04-14

@ -93,6 +93,35 @@ abc
.Should().ContainSingle(evt => evt.RenderMessage(default) == expectedLog);
}
[Test]
public void Parse_SkippableLines_AreSkippedWithLog()
{
var markdown = StringUtils.TrimmedString(@"
# First Release Profile
!!! Admonition lines are skipped
Indented lines are skipped
");
// List of substrings of logs that should appear in the resulting list of logs after parsing is done.
// We are only looking for logs relevant to the skipped lines we're testing for.
var expectedLogs = new List<string>
{
"Skip Admonition",
"Skip Indented Line"
};
var context = new Context();
var results = context.GuideParser.ParseMarkdown(context.Config.ReleaseProfiles.First(), markdown);
results.Should().BeEmpty();
var ctx = TestCorrelator.GetLogEventsFromCurrentContext().ToList();
foreach (var log in expectedLogs)
{
ctx.Should().Contain(evt => evt.MessageTemplate.Text.Contains(log));
}
}
[Test]
public void Parse_StrictNegativeScores()
{

@ -5,6 +5,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json.Linq;
using Serilog;
using Trash.Extensions;
@ -51,7 +52,7 @@ namespace Trash.Sonarr.ReleaseProfile
for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
state.LineNumber++;
if (string.IsNullOrEmpty(line))
if (IsSkippableLine(line))
{
continue;
}
@ -91,6 +92,31 @@ namespace Trash.Sonarr.ReleaseProfile
return results;
}
private bool IsSkippableLine(string line)
{
if (string.IsNullOrEmpty(line))
{
return true;
}
// Skip lines with leading whitespace (i.e. indentation).
// These lines will almost always be `!!! attention` blocks of some kind and won't contain useful data.
if (char.IsWhiteSpace(line, 0))
{
Log.Debug(" - Skip Indented Line: {Line}", line);
return true;
}
// Lines that begin with `???` or `!!!` are admonition syntax (extended markdown supported by Python)
if (line.StartsWith("!!!") || line.StartsWith("???"))
{
Log.Debug(" - Skip Admonition: {Line}", line);
return true;
}
return false;
}
private static Regex BuildRegex(string regex)
{
return new(regex, RegexOptions.Compiled | RegexOptions.IgnoreCase);

@ -4,21 +4,21 @@ on how the python script is implemented currently.
# Definitions
* **Term**<br>
- **Term**<br>
A phrase that is included in Sonarr release profiles under either the "Preferred", "Must Contain",
or "Must Not Contain" sections. In the TRaSH guides these are regular expressions.
* **Ignored**<br>
- **Ignored**<br>
The API term for "Must Not Contain"
* **Required**<br>
- **Required**<br>
The API term for "Must Contain"
* **Category**<br>
- **Category**<br>
Refers to any of the different "sections" in a release profile where terms may be stored. Includes
"Must Not Contain" (ignored), "Must Contain" (required), and "Preferred".
* **Mention**<br>
- **Mention**<br>
This generally refers to any human-readable way of stating something that the script relies on for
parsing purposes.
@ -33,6 +33,12 @@ Note that all parsing happens directly on the markdown files themselves from the
repository. Those files are processed one line at a time. Guidelines will apply on a per-line basis,
unless otherwise stated.
The following general rules apply to all lines in the markdown data:
- Lines with leading whitespace (indentation) are skipped
- Blank lines are skipped
- Admonition lines (starting with `!!!` or `???`) are skipped.
## Sonarr Release Profiles
1. **Headers define release profiles.**
@ -68,9 +74,9 @@ unless otherwise stated.
A category must mentioned as one of the following phrases (case insensitive):
* `Preferred`
* `Must Not Contain`
* `Must Contain`
- `Preferred`
- `Must Not Contain`
- `Must Contain`
These phrases may appear in nested headers, normal lines, and may even appear inside the same
line that defines a score (e.g. `Insert these as "Preferred" with a score of [100]`).
@ -94,7 +100,7 @@ The script procedurally generates a name for release profiles it creates. For th
The name is generated as follows:
* `Anime` comes from the guide type (could be `WEB-DL`)
* `First Release Profile` is directly from one of the headers in the anime guide
* `[Trash]` is used by the script to mean "This release profile is controlled by the script". This
- `Anime` comes from the guide type (could be `WEB-DL`)
- `First Release Profile` is directly from one of the headers in the anime guide
- `[Trash]` is used by the script to mean "This release profile is controlled by the script". This
is to separate it from any manual ones the user has defined, which the script will not touch.

Loading…
Cancel
Save