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

support multiple region selection in code snippet #246

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 14 additions & 9 deletions Documentation/spec/docfx_flavored_markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ Block file inclusion must be in a single line and with no prefix characters befo
User may need to define section. Mostly used for code table.
Give an example below.

> [!div class="tabbedCodeSnippets" data-resources="OutlookServices.Calendar"]
> ```cs
> <cs code text>
> ```
> ```javascript
> <js code text>
> ```
[!div class="tabbedCodeSnippets" data-resources="OutlookServices.Calendar"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

div should be in blocks? why removing >

```cs
<cs code text>
```
```javascript
<js code text>
```

The above blockquote Markdown text will transform to section html as in the following:
```
Expand All @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 query string options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 option: # and ?, and option? has 3 sub options ....
I think it's more clear if I use a table.

* __`#`__: _`#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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ from tag in tagGroup
tagResolveResult.StartLine = startLine + 1;
tagResolveResult.EndLine = endLine - 1;
tagResolveResult.ExcludesLines = excludedLines;
tagResolveResult.IndentLength = DfmCodeExtractorHelper.GetIndentLength(lines[startLine - 1]);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
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.

Original file line number Diff line number Diff line change
@@ -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;
}

}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
}
}
Loading