Skip to content

Commit

Permalink
Merge pull request #3 from Toine-db/text_to_segments
Browse files Browse the repository at this point in the history
Text in segments and .Net 9 support
  • Loading branch information
Toine-db authored Nov 13, 2024
2 parents 69058ee + 649f73a commit 025260c
Show file tree
Hide file tree
Showing 28 changed files with 815 additions and 219 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Build
run: dotnet build test\MarkdownParser.Test.sln -c Release
- name: Run Tests
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Build
run: dotnet build src\MarkdownParser.sln -c Release
run: dotnet build src\MarkdownParser.sln -c Release
38 changes: 16 additions & 22 deletions src/MarkdownParser/IViewSupplier.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
using System.Collections.Generic;
using MarkdownParser.Models;

namespace MarkdownParser
{
public interface IViewSupplier<T>
{
/// <summary>
/// get a textual line break
/// registering reference definitions (sometimes at the end of the document or used for creating links)
/// </summary>
/// <param name="markdownReferenceDefinitions">collection of Reference Definitions</param>
/// <returns></returns>
string GetTextualLineBreak();
void RegisterReferenceDefinitions(IEnumerable<MarkdownReferenceDefinition> markdownReferenceDefinitions);

/// <summary>
/// a default text view
/// </summary>
/// <param name="content"></param>
/// <param name="textBlock">textBlock with a collection of text segments</param>
/// <returns></returns>
T GetTextView(string content);
T GetTextView(TextBlock textBlock);

/// <summary>
/// a block quote view, where other views can be inserted
Expand All @@ -27,13 +29,13 @@ public interface IViewSupplier<T>
/// <summary>
/// a title, subtitle or header view
/// </summary>
/// <param name="content"></param>
/// <param name="headerLevel"></param>
/// <param name="textBlock">textBlock with a collection of text segments</param>
/// <param name="headerLevel">header level</param>
/// <returns></returns>
T GetHeaderView(string content, int headerLevel);
T GetHeaderView(TextBlock textBlock, int headerLevel);

/// <summary>
/// a image view with a optional subscription text view
/// a image view with an optional subscription text view
/// </summary>
/// <param name="url">image location</param>
/// <param name="subscription">(optional) null or empty when not used</param>
Expand All @@ -42,7 +44,7 @@ public interface IViewSupplier<T>
T GetImageView(string url, string subscription, string imageId);

/// <summary>
/// a view that shows a list of listitems
/// a view that shows a list of list-items
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
Expand All @@ -51,7 +53,7 @@ public interface IViewSupplier<T>
/// <summary>
/// a view that shows a single item for a ListView (return View can be used in GetListView)
/// </summary>
/// <param name="childView">view as childview (or use the content parameter)</param>
/// <param name="childView">view as child-view (or use the content parameter)</param>
/// <param name="isOrderedList">does the item belong to a ordered (numbered) list</param>
/// <param name="sequenceNumber">number of sequence</param>
/// <param name="listLevel">level depth of the list, root level starting at 1</param>
Expand All @@ -66,12 +68,11 @@ public interface IViewSupplier<T>
T GetStackLayoutView(List<T> childViews);

/// <summary>
/// a image view that separates content
/// an image view that separates content
/// </summary>
/// <returns></returns>
T GetThematicBreak();


/// <summary>
/// a placeholder for views or other objects
/// </summary>
Expand All @@ -83,25 +84,18 @@ public interface IViewSupplier<T>
/// a view that shows fenced code (found in MD blocks starting with ```cs )
/// </summary>
/// <returns></returns>
T GetFencedCodeBlock(string content, string codeInfo);
T GetFencedCodeBlock(TextBlock textBlock, string codeInfo);

/// <summary>
/// a view that shows indented code (found in MD lines starting with at least 4 spaces)
/// </summary>
/// <returns></returns>
T GetIndentedCodeBlock(string content);
T GetIndentedCodeBlock(TextBlock textBlock);

/// <summary>
/// a view that shows html content
/// </summary>
/// <returns></returns>
T GetHtmlBlock(string content);

/// <summary>
/// a view that shows reference definitions ([link]s usually at the end of the document)
/// </summary>
/// <param name="markdownReferenceDefinitions">collection of Reference Definitions</param>
/// <returns></returns>
T GetReferenceDefinitions(IEnumerable<MarkdownReferenceDefinition> markdownReferenceDefinitions);
T GetHtmlBlock(TextBlock textBlock);
}
}
1 change: 1 addition & 0 deletions src/MarkdownParser/MarkdownParseStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using CommonMark.Syntax;
using MarkdownParser.Writer;

namespace MarkdownParser
{
Expand Down
20 changes: 19 additions & 1 deletion src/MarkdownParser/MarkdownParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using CommonMark;
using CommonMark.Syntax;
using MarkdownParser.Writer;

namespace MarkdownParser
{
Expand All @@ -14,6 +15,11 @@ public MarkdownParser(IViewSupplier<T> viewSupplier)
_viewSupplier = viewSupplier;
}

/// <summary>
/// Format\Convert markdown into UI Components
/// </summary>
/// <param name="markdownSource"></param>
/// <returns></returns>
public List<T> Parse(string markdownSource)
{
using (var reader = new StringReader(markdownSource))
Expand All @@ -22,6 +28,11 @@ public List<T> Parse(string markdownSource)
}
}

/// <summary>
/// Format\Convert markdown into UI Components
/// </summary>
/// <param name="markdownSource"></param>
/// <returns></returns>
public List<T> Parse(TextReader markdownSource)
{
// Parse to usable c# objects
Expand All @@ -34,11 +45,18 @@ public List<T> Parse(TextReader markdownSource)
return uiComponents;
}

public static Block GetMarkdownDocument(TextReader markdownSource)
/// <summary>
/// Get usable c# objects from
/// </summary>s
/// <param name="markdownSource"></param>
/// <returns></returns>
internal static Block GetMarkdownDocument(TextReader markdownSource)
{
// Parse to usable c# objects
var settings = CommonMarkSettings.Default.Clone();
settings.AdditionalFeatures |= CommonMarkAdditionalFeatures.PlaceholderBracket;
settings.AdditionalFeatures |= CommonMarkAdditionalFeatures.StrikethroughTilde;

return CommonMarkConverter.Parse(markdownSource, settings);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/MarkdownParser/MarkdownParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>

<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>

<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<!-- NuGet -->
<Authors>Toine de Boer</Authors>
<Copyright>Copyright © Toine de Boer and contributors</Copyright>
Expand Down Expand Up @@ -46,7 +47,7 @@

<ItemGroup>
<None Include="..\..\nuget.png" PackagePath="icon.png" Pack="true" />
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace MarkdownParser
namespace MarkdownParser.Models
{
public class MarkdownReferenceDefinition
{
Expand Down
12 changes: 12 additions & 0 deletions src/MarkdownParser/Models/Segments/BaseTextSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace MarkdownParser.Models.Segments
{
public abstract class BaseSegment
{
public bool HasLiteralContent { get; protected set; } = false;

public override string ToString()
{
return string.Empty;
}
}
}
21 changes: 21 additions & 0 deletions src/MarkdownParser/Models/Segments/IndicatorSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MarkdownParser.Models.Segments.Indicators;

namespace MarkdownParser.Models.Segments
{
public class IndicatorSegment : BaseSegment
{
public SegmentIndicator Indicator { get; }
public SegmentIndicatorPosition IndicatorPosition { get; }

public IndicatorSegment(SegmentIndicator indicator, SegmentIndicatorPosition indicatorPosition)
{
Indicator = indicator;
IndicatorPosition = indicatorPosition;
}

public override string ToString()
{
return string.Empty;
}
}
}
13 changes: 13 additions & 0 deletions src/MarkdownParser/Models/Segments/Indicators/SegmentIndicator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MarkdownParser.Models.Segments.Indicators
{
public enum SegmentIndicator
{
NotSupported,
Strong,
Italic,
Strikethrough,
Code,
Link,
LineBreak
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MarkdownParser.Models.Segments.Indicators
{
public enum SegmentIndicatorPosition
{
Start,
End
}
}
17 changes: 17 additions & 0 deletions src/MarkdownParser/Models/Segments/LinkSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MarkdownParser.Models.Segments.Indicators;

namespace MarkdownParser.Models.Segments
{
public class LinkSegment : IndicatorSegment
{
public string Url { get; }
public string Title { get; }

public LinkSegment(SegmentIndicator indicator, SegmentIndicatorPosition indicatorPosition, string url, string title)
: base(indicator, indicatorPosition)
{
Url = url;
Title = title;
}
}
}
18 changes: 18 additions & 0 deletions src/MarkdownParser/Models/Segments/TextSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace MarkdownParser.Models.Segments
{
public class TextSegment : BaseSegment
{
public string Text { get; set; }

public TextSegment(string text)
{
Text = text;
HasLiteralContent = !string.IsNullOrWhiteSpace(Text);
}

public override string ToString()
{
return Text ?? string.Empty;
}
}
}
25 changes: 25 additions & 0 deletions src/MarkdownParser/Models/TextBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using MarkdownParser.Models.Segments;

namespace MarkdownParser.Models
{
public class TextBlock
{
public BaseSegment[] TextSegments { get; }

public TextBlock(BaseSegment[] textSegments)
{
TextSegments = textSegments;
}

/// <summary>
/// Get all text (LiteralContent) and ignoring all emphasize
/// </summary>
/// <param name="usedStringForLineBreaks">text used for line breaks</param>
/// <returns>clean text</returns>
public string ExtractLiteralContent(string usedStringForLineBreaks)
{
var literalContent = TextSegmentHelper.TextSegmentsToLiteralContent(TextSegments, usedStringForLineBreaks);
return literalContent;
}
}
}
35 changes: 35 additions & 0 deletions src/MarkdownParser/Models/TextSegmentHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using System.Text;
using MarkdownParser.Models.Segments;
using MarkdownParser.Models.Segments.Indicators;

namespace MarkdownParser.Models
{
public static class TextSegmentHelper
{
public static string TextSegmentsToLiteralContent(BaseSegment[] segments, string optionalLineBreak)
{
var literalContentBuilder = new StringBuilder();

if (segments == null
|| !segments.Any(x => x.HasLiteralContent))
{
return string.Empty;
}

for (var i = 0; i < segments.Length; i++)
{
var literalContent = segments[i].ToString();
if (segments[i] is IndicatorSegment indicatorSegment
&& indicatorSegment.Indicator == SegmentIndicator.LineBreak)
{
literalContent = optionalLineBreak;
}

literalContentBuilder.Append(literalContent);
}

return literalContentBuilder.ToString();
}
}
}
Loading

0 comments on commit 025260c

Please sign in to comment.