Skip to content

Commit

Permalink
Fixes issues with comments and keywords in Paket highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
edhzsz authored and Edgar Hernandez committed May 26, 2016
1 parent 5b48d83 commit 576dbcd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
26 changes: 16 additions & 10 deletions src/CSharpFormat/PaketFormat.cs
Expand Up @@ -11,13 +11,16 @@ public class PaketFormat : FSharpFormat
{
/// <summary>
/// Regular expression string to match single line and multi-line
/// comments (// and (* *)). Single line comments should not have
/// a : before them to avoid color as comments URLs. For example
/// (source https://nuget.org/api/v2)
/// comments (// and (* *)). Single line comments should have to have
/// a space after them to avoid color as comments URLs and paths. For example
/// ```
/// source https://nuget.org/api/v2
// cache //hive/dependencies
/// ```
/// </summary>
protected override string CommentRegEx
{
get { return @"\(\*.*?\*\)|(?<!\:|\:/)//.*?(?:\r|\n|$)"; }
get { return @"\(\*.*?\*\)|//\s.*?(?:\r|\n|$)"; }
}

/// <summary>
Expand All @@ -33,8 +36,13 @@ protected override string Operators
/// </summary>
protected override string Keywords
{
get { return "source nuget\\s github\\s gist\\s git\\s http\\s group framework version_in_path content "
+ "copy_local redirects import_targets references cache strategy lowest_matching NUGET GITHUB GROUP GIT HTTP specs remote File"; }
get
{
return "source nuget github gist git http group framework version_in_path content"
+ " copy_local redirects import_targets references cache strategy lowest_matching NUGET"
+ " specs remote File username password copy_content_to_output_dir GITHUB GROUP GIT HTTP"
+ " CopyToOutputDirectory";
}
}

/// <summary>
Expand Down Expand Up @@ -65,11 +73,9 @@ protected string BuildKeywordsRegex(string separated)
{
if (separated.Length == 0) return "";
var sb = new StringBuilder(separated);

Sanitize(sb);

sb.Replace(" ", @"\b|\b");
return @"\b" + sb.ToString() + @"\b";
sb.Replace(" ", @"(?=\:?(?:\s|$))|(?<=\s|^)");
return @"(?<=\s|^)" + sb.ToString() + @"(?=\:?(?:\s|$))";
}
}
}
30 changes: 25 additions & 5 deletions tests/FSharp.Literate.Tests/Tests.fs
Expand Up @@ -275,13 +275,22 @@ let ``Correctly encodes already encoded HTML entities and tags`` () =
)

[<Test>]
let ``Urls are not recognized as comments in Paket code blocks`` () =
let ``Urls should not be recognized as comments in Paket code blocks`` () =
let content = """
[lang=packet]
source https://nuget.org/api/v2"""
let doc = Literate.ParseMarkdownString(content, formatAgent=getFormatAgent())
let html = Literate.WriteHtml(doc)
html |> should contain @"https://nuget.org/api/v2"

[<Test>]
let ``Path to network share should not be recognized as comments in Paket code blocks`` () =
let content = """
[lang=packet]
cache //hive/dependencies"""
let doc = Literate.ParseMarkdownString(content, formatAgent=getFormatAgent())
let html = Literate.WriteHtml(doc)
html |> should notContain "<span class=\"c\">//hive/dependencies</span>"

[<Test>]
let ``Correctly handles Paket coloring`` () =
Expand All @@ -292,9 +301,12 @@ let ``Correctly handles Paket coloring`` () =
content: none
import_targets: false
copy_local: false
copy_content_to_output_dir: always
redirects: on
strategy: min
lowest_matching: true
source https://nuget.org/api/v2 // nuget.org
cache //hive/dependencies
// NuGet packages
nuget NUnit ~> 2.6.3
Expand All @@ -308,6 +320,7 @@ let ``Correctly handles Paket coloring`` () =
// Files from GitHub repositories
github forki/FsUnit FsUnit.fs
git https://github.com/fsprojects/Paket.git master
// Gist files
gist Thorium/1972349 timestamp.fs
Expand All @@ -321,19 +334,25 @@ let ``Correctly handles Paket coloring`` () =
let doc = Literate.ParseMarkdownString(content, formatAgent=getFormatAgent())
let html = Literate.WriteHtml(doc)

html |> should contain "<span class=\"k\">nuget </span>"
html |> should contain "<span class=\"k\">github </span>"
html |> should contain "<span class=\"k\">gist </span>"
html |> should contain "<span class=\"k\">http </span>"
html |> should contain "<span class=\"k\">nuget</span>"
html |> should contain "<span class=\"k\">github</span>"
html |> should contain "<span class=\"k\">git</span>"
html |> should contain "<span class=\"k\">gist</span>"
html |> should contain "<span class=\"k\">http</span>"
html |> should contain "<span class=\"k\">references</span>"
html |> should contain "<span class=\"k\">framework</span>"
html |> should contain "<span class=\"k\">content</span>"
html |> should contain "<span class=\"k\">import_targets</span>"
html |> should contain "<span class=\"k\">copy_local</span>"
html |> should contain "<span class=\"k\">copy_content_to_output_dir</span>"
html |> should contain "<span class=\"k\">lowest_matching</span>"
html |> should contain "<span class=\"k\">redirects</span>"
html |> should contain "<span class=\"k\">strategy</span>"
html |> should contain "<span class=\"k\">version_in_path</span>"

html |> should notContain "<span class=\"k\">http</span>s"
html |> should notContain ".<span class=\"k\">git</span>"

html |> should contain "<span class=\"o\">~&gt;</span>"
html |> should contain "<span class=\"o\">&gt;=</span>"
html |> should contain "<span class=\"o\">==</span>"
Expand All @@ -346,6 +365,7 @@ let ``Correctly handles Paket coloring`` () =

html |> should contain "<span class=\"c\">// NuGet packages</span>"
html |> should contain "<span class=\"c\">// nuget.org</span>"
html |> should notContain "<span class=\"c\">//hive/dependencies</span>"

html |> should contain @"https://nuget.org/api/v2"
html |> should contain @"http://www.fssnip.net/1n"
Expand Down

0 comments on commit 576dbcd

Please sign in to comment.