Navigation Menu

Skip to content

Commit

Permalink
Support multiple region selection in code snippet #189
Browse files Browse the repository at this point in the history
  • Loading branch information
superyyrrzz committed Apr 12, 2016
1 parent c783135 commit 3888915
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 319 deletions.
9 changes: 7 additions & 2 deletions Documentation/spec/docfx_flavored_markdown.md
Expand Up @@ -110,8 +110,12 @@ Allows you to insert code with code language specified. The content of specified
* __`<language>`__ can be made up of any number of character and '-'. However, the recommended value should follow [Highlight.js language names and aliases](http://highlightjs.readthedocs.org/en/latest/css-classes-reference.html#language-names-and-aliases).
* __`<codepath>`__ is the relative path in file system which indicates the code snippet file that you want to expand.
* __`<queryoption>`__ and __`<queryoptionvalue>`__ are used together to retrieve part of the code snippet file in the line range or tag name way. We have 2 query string options to represent these two ways:
* __`#`__: _`#L{startlinenumber}-L{endlinenumber}`_ (line range) or _`#L{tagname}`_ (tag name)
* __`?`__: _`?start={startlinenumber}&end={endlinenumber}`_ (line range) or _`?{name}={tagname}`_ (tag name)

| | query string using `#` | query string using `?`
|--------------------------|----------------------------------------|-------------
| 1. line range | `#L{startlinenumber}-L{endlinenumber}` | `?start={startlinenumber}&end={endlinenumber}`
| 2. tagname | `#{tagname}` | `?name={tagname}`
| 3. multiple region range | _Unsupported_ | `?range={rangequerystring}`
* __`<title>`__ can be omitted.

#### Code Snippet Sample
Expand All @@ -123,6 +127,7 @@ Allows you to insert code with code language specified. The content of specified

[!code[Main](index.xml?start=5&end=9)]
[!code-javascript[Main](../jquery.js?name=testsnippet)]
[!code[Main](index.xml?range=2,5-7,9-) "This includes the lines 2, 5, 6, 7 and lines 9 to the last line"]
```

#### Tag Name Representation in Code Snippet Source File
Expand Down
10 changes: 0 additions & 10 deletions src/Microsoft.DocAsCode.Dfm/CodeSnippetExtractor/CodeSnippet.cs
Expand Up @@ -3,16 +3,6 @@

namespace Microsoft.DocAsCode.Dfm
{
using System;
using System.Collections.Generic;

public class CodeSnippet
{
public string Name { get; set; }

public List<Tuple<int, int>> LinePair { get; set; }
}

public class CodeSnippetTag
{
public CodeSnippetTag(string name, int line, CodeSnippetTagType type)
Expand Down
Expand Up @@ -48,7 +48,6 @@ public abstract class CodeSnippetRegexExtractor : ICodeSnippetExtractor
tagResolveResult.StartLine = startLine + 1;
tagResolveResult.EndLine = endLine - 1;
tagResolveResult.ExcludesLines = excludedLines;
tagResolveResult.IndentLength = DfmCodeExtractorHelper.GetIndentLength(lines[startLine - 1]);
}
else
{
Expand Down
Expand Up @@ -11,8 +11,6 @@ public class DfmTagNameResolveResult

public int EndLine { get; set; }

public int IndentLength { get; set; }

public HashSet<int> ExcludesLines { get; set; }

public bool IsSuccessful { get; set; }
Expand Down
291 changes: 12 additions & 279 deletions src/Microsoft.DocAsCode.Dfm/DfmCodeExtractor.cs

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions src/Microsoft.DocAsCode.Dfm/DfmFencesBlockPathQueryOption.cs

This file was deleted.

@@ -0,0 +1,46 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.Dfm
{
using System.Collections.Generic;

public abstract class DfmFencesBlockPathQueryOption : IDfmFencesBlockPathQueryOption
{
public string ErrorMessage { get; protected set; }

public abstract bool ValidateAndPrepare(string[] lines, DfmFencesBlockToken token);

public abstract IEnumerable<string> GetQueryLines(string[] lines);

protected bool CheckLineRange(int totalLines, int? startLine, int? endLine)
{
if (startLine == null && endLine == null)
{
ErrorMessage = "Neither start line nor end line is specified correctly";
return false;
}

if (startLine <= 0 || endLine <= 0)
{
ErrorMessage = "Start/End line should be larger than zero";
return false;
}

if (startLine > endLine)
{
ErrorMessage = $"Start line {startLine} shouldn't be larger than end line {endLine}";
return false;
}

if (startLine > totalLines)
{
ErrorMessage = $"Start line '{startLine}' execeeds total file lines '{totalLines}'";
return false;
}

return true;
}

}
}
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.Dfm
{
using System.Collections.Generic;

public interface IDfmFencesBlockPathQueryOption
{
string ErrorMessage { get; }

bool ValidateAndPrepare(string[] lines, DfmFencesBlockToken token);

IEnumerable<string> GetQueryLines(string[] lines);
}
}
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.Dfm
{
using System;
using System.Collections.Generic;

class LineRangeBlockPathQueryOption : DfmFencesBlockPathQueryOption
{
public int? StartLine { get; set; }

public int? EndLine { get; set; }

public override bool ValidateAndPrepare(string[] lines, DfmFencesBlockToken token)
{
if (!CheckLineRange(lines.Length, StartLine, EndLine))
{
return false;
}

return true;
}

public override IEnumerable<string> GetQueryLines(string[] lines)
{
int startLine = StartLine ?? 1;
int endLine = EndLine ?? lines.Length;

for (int i = startLine; i <= Math.Min(endLine, lines.Length); i++)
{
yield return lines[i - 1];
}
}
}
}
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.Dfm
{
using System;
using System.Collections.Generic;

class MultipleLineRangeBlockPathQueryOption : DfmFencesBlockPathQueryOption
{
public List<Tuple<int?, int?>> LinePairs { get; set; } = new List<Tuple<int?, int?>>();

public override bool ValidateAndPrepare(string[] lines, DfmFencesBlockToken token)
{
foreach (var pair in LinePairs)
{
if (!CheckLineRange(lines.Length, pair.Item1, pair.Item2))
{
return false;
}
}

return true;
}

public override IEnumerable<string> GetQueryLines(string[] lines)
{
foreach (var pair in LinePairs)
{
int startLine = pair.Item1 ?? 1;
int endLine = pair.Item2 ?? lines.Length;

for (int i = startLine; i <= Math.Min(endLine, lines.Length); i++)
{
yield return lines[i - 1];
}
}
}
}
}

0 comments on commit 3888915

Please sign in to comment.