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

Andy840119/ add codeblock syntex highlighting #1

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
711b259
fix CodeBlock contain "[]" will cause crash
andy840119 Dec 8, 2018
f9c3823
convert relative url to absolute url
andy840119 Dec 8, 2018
54f2356
oh i forgot Appvery
andy840119 Dec 8, 2018
09b11de
Merge branch 'master' of https://github.com/ppy/osu-framework into an…
andy840119 Dec 13, 2018
1cf2943
i don't know why but sometimes markdig made some additional empty line
andy840119 Dec 13, 2018
19297c2
hmm...
andy840119 Dec 13, 2018
2c45edd
oops what am i writing ODOa
andy840119 Dec 16, 2018
008ec27
Merge branch 'master' into andy840119/_fix_markdown_crash_and_relativ…
andy840119 Dec 16, 2018
74b2a01
almost there
andy840119 Dec 23, 2018
4c85d22
change format
andy840119 Dec 23, 2018
8cc7b2a
1. make it compilable
andy840119 Dec 23, 2018
5b3d7f3
modified some coding style
andy840119 Dec 23, 2018
0d2c531
Foregound -> Colour
andy840119 Dec 23, 2018
5710025
make appveyor happy
andy840119 Dec 23, 2018
2147b9f
makes codefactor happy
andy840119 Dec 23, 2018
c04110e
Merge branch 'master' of https://github.com/ppy/osu-framework into an…
andy840119 Dec 24, 2018
a9f3f0e
Merge branch 'andy840119/_fix_markdown_crash_and_relative_path_issue'…
andy840119 Dec 24, 2018
94af4d3
Merge branch 'master' of https://github.com/osu-Karaoke/osu-framework…
andy840119 Jan 8, 2019
6c47492
fix code quality issue.
andy840119 Jan 8, 2019
6415682
Merge branch 'andy840119/_fix_markdown_crash_and_relative_path_issue'…
andy840119 Jan 8, 2019
5d689e3
make appveyor happy again
andy840119 Jan 8, 2019
6cbb718
Merge branch 'master' into andy840119/_add_codeblock_syntex_highlighting
andy840119 Jan 29, 2019
9f41b5b
Merge branch 'master' of https://github.com/osu-Karaoke/osu-framework…
andy840119 Mar 1, 2019
3feb256
Merge branch 'andy840119/_fix_markdown_crash_and_relative_path_issue'…
andy840119 Mar 1, 2019
cead122
fix issue appveyor hint
andy840119 Mar 1, 2019
de82fab
change font usage
andy840119 Mar 1, 2019
25000ec
fix broken code
andy840119 Mar 1, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ public TestCaseMarkdown()
```";
});

AddStep("Markdown Fenced Code (CSharp)", () =>
{
markdownContainer.Text = @"```csharp
// A Hello World! program in C#.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine(""Hello World!"");

// Keep the console window open in debug mode.
Console.WriteLine(""Press any key to exit."");
Console.ReadKey();
}
}
}
```";
});

AddStep("Markdown Table", () =>
{
markdownContainer.Text =
Expand Down Expand Up @@ -112,7 +134,9 @@ public TestCaseMarkdown()

AddStep("MarkdownFromInternet", () =>
{
var req = new WebRequest("https://raw.githubusercontent.com/ppy/osu-wiki/master/wiki/Skinning/skin.ini/en.md");
const string url = "https://raw.githubusercontent.com/ppy/osu-wiki/master/wiki/Skinning/skin.ini/en.md";
markdownContainer.RootUrl = url;
var req = new WebRequest(url);
req.Finished += () => markdownContainer.Text = req.ResponseString;

Task.Run(() => req.PerformAsync());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Framework.Graphics.Containers.Markdown
{
public interface IMarkdownCodeFlowComponent
{
/// <summary>
/// Creates a <see cref="MarkdownCodeFlowContainer"/> to display text within this <see cref="IMarkdownTextFlowComponent"/>.
/// </summary>
/// <remarks>
/// The <see cref="MarkdownCodeFlowContainer"/> defined by the <see cref="MarkdownCodeFlowContainer"/> is used by default,
/// but may be overridden via this method to provide additional styling local to this <see cref="IMarkdownTextFlowComponent"/>.
/// </remarks>
/// <returns>The <see cref="MarkdownCodeFlowContainer"/>.</returns>
MarkdownCodeFlowContainer CreateCodeFlow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using ColorCode;
using ColorCode.Parsing;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using System;
using System.Collections.Generic;
using System.Linq;

namespace osu.Framework.Graphics.Containers.Markdown
{
public class MarkdownCodeFlowContainer : CustomizableTextContainer, IMarkdownTextComponent
{
[Resolved]
private IMarkdownTextComponent parentTextComponent { get; set; }

public MarkdownCodeFlowContainer()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
}

public new void AddText(string text, Action<SpriteText> creationParameters = null)
=> base.AddText(text.Replace("[", "[[").Replace("]", "]]"), creationParameters);

public void AddCodeText(string codeText,string languageCode)
{
if(string.IsNullOrEmpty(codeText))
AddParagraph("");

var formatter = new ClassFormatter(Styles);

var language = Languages.FindById(languageCode);
if (language == null)
AddParagraph(codeText);
else
{
//Change new line
AddParagraph("");

var codeSyntaxes = formatter.GetCodeSyntaxes(codeText, language);

foreach (var codeSyntax in codeSyntaxes)
AddText(codeSyntax.ParsedSourceCode,
x =>
{
var style = codeSyntax.CodeStyle;
if(style == null)
return;

ApplyCodeText(x, style);
});
}
}

SpriteText IMarkdownTextComponent.CreateSpriteText() => CreateSpriteText();

protected virtual MarkdownCodeStyle Styles => new MarkdownCodeStyle().CreateDefaultStyle();

protected virtual void ApplyCodeText(SpriteText text, MarkdownCodeStyle.Style codeStyle)
{
var textDrawable = CreateSpriteText();

text.Colour = codeStyle.Colour;
text.ShadowColour = codeStyle.BackgroundColour ?? text.ShadowColour;
text.Font = textDrawable.Font.With(weight: codeStyle.Bold ? "Bold" : null, italics: codeStyle.Italic);
}

public class ClassFormatter : CodeColorizerBase
{
private readonly List<CodeSyntax> codeSyntaxes = new List<CodeSyntax>();

private readonly MarkdownCodeStyle markdownCodeStyle;

public ClassFormatter(MarkdownCodeStyle style, ILanguageParser languageParser = null) : base(null, languageParser)
{
markdownCodeStyle = style;
}

public List<CodeSyntax> GetCodeSyntaxes(string sourceCode, ILanguage language)
{
codeSyntaxes.Clear();
languageParser.Parse(sourceCode, language, Write);
return codeSyntaxes;
}

protected override void Write(string parsedSourceCode, IList<Scope> scopes)
{
var scopeName = scopes.FirstOrDefault()?.Name;
var style = new MarkdownCodeStyle.Style("unknown");

if (!string.IsNullOrEmpty(scopeName) && markdownCodeStyle.Contains(scopeName))
style = markdownCodeStyle[scopeName];

codeSyntaxes.Add(new CodeSyntax
{
ParsedSourceCode = parsedSourceCode,
CodeStyle = style
});
}
}

public class CodeSyntax
{
/// <summary>
/// Gets or sets the parsed source code
/// </summary>
/// <value>The background color.</value>
public string ParsedSourceCode { get; set; }

/// <summary>
/// Gets or sets the parsed code style
/// </summary>
public MarkdownCodeStyle.Style CodeStyle { get; set; }
}
}
}
Loading