Add comment for helpers

This commit is contained in:
Alexandre Mutel
2016-03-04 23:43:26 +09:00
parent 5aea549f3b
commit ba5527e7b2
10 changed files with 143 additions and 14 deletions

View File

@@ -7,6 +7,9 @@ using System.Runtime.CompilerServices;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// Helper class for handling characters.
/// </summary>
public static class CharHelper
{
public const int TabSize = 4;

View File

@@ -1,11 +1,44 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
// Original file released under the following license:
// Copyright(c) 2014, Kārlis Gaņģis
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of Kārlis Gaņģis nor the names of other contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Generic;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// Helper class to decode an entity.
/// </summary>
public static class EntityHelper
{
/// <summary>
@@ -16,7 +49,7 @@ namespace Textamina.Markdig.Helpers
public static string DecodeEntity(string entity)
{
string result;
if (_entityMap.TryGetValue(entity, out result))
if (EntityMap.TryGetValue(entity, out result))
return result;
return null;
@@ -42,11 +75,11 @@ namespace Textamina.Markdig.Helpers
});
}
#region [ _entityMap ]
#region [ EntityMap ]
/// <summary>
/// Source: http://www.w3.org/html/wg/drafts/html/master/syntax.html#named-character-references
/// </summary>
private static readonly Dictionary<string, string> _entityMap = new Dictionary<string, string>(2125, StringComparer.Ordinal)
private static readonly Dictionary<string, string> EntityMap = new Dictionary<string, string>(2125, StringComparer.Ordinal)
{
{ "Aacute", "\u00C1" },
{ "aacute", "\u00E1" },

View File

@@ -1,12 +1,43 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
// Part of this file was released under the following license:
// Copyright(c) 2014, Kārlis Gaņģis
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of Kārlis Gaņģis nor the names of other contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Text;
using Textamina.Markdig.Syntax;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// Helper to parse several HTML tags.
/// </summary>
public static class HtmlHelper
{
private static readonly char[] SearchBackAndAmp = { '\\', '&' };
@@ -533,6 +564,7 @@ namespace Textamina.Markdig.Helpers
/// </summary>
public static int ScanEntity(string s, int pos, int length, out string namedEntity, out int numericEntity)
{
/*!re2c
[&] ([#] ([Xx][A-Fa-f0-9]{1,8}|[0-9]{1,8}) |[A-Za-z][A-Za-z0-9]{1,31} ) [;]
{ return (p - start); }

View File

@@ -6,6 +6,9 @@ using Textamina.Markdig.Syntax;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// Helpers to parse Markdown links.
/// </summary>
public static class LinkHelper
{
public static bool TryParseAutolink(StringSlice text, out string link, out bool isEmail)

View File

@@ -5,6 +5,9 @@ using System.Runtime.CompilerServices;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// Internal helper to allow to declare a method using AggressiveInlining without being .NET 4.0+
/// </summary>
internal static class MethodImplOptionPortable
{
public const MethodImplOptions AggressiveInlining = (MethodImplOptions)256;

View File

@@ -6,15 +6,26 @@ using System.Collections.Generic;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// A simple object recycling system.
/// </summary>
/// <typeparam name="T">Type of the object to cache</typeparam>
public class ObjectCache<T> where T : class, new()
{
private readonly Stack<T> builders;
/// <summary>
/// Initializes a new instance of the <see cref="ObjectCache{T}"/> class.
/// </summary>
public ObjectCache()
{
builders = new Stack<T>();
}
/// <summary>
/// Gets a new instance.
/// </summary>
/// <returns></returns>
public T Get()
{
lock (builders)
@@ -28,17 +39,26 @@ namespace Textamina.Markdig.Helpers
return new T();
}
public void Release(T builder)
/// <summary>
/// Releases the specified instance.
/// </summary>
/// <param name="instance">The instance.</param>
/// <exception cref="System.ArgumentNullException">if instance is null</exception>
public void Release(T instance)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
Reset(builder);
if (instance == null) throw new ArgumentNullException(nameof(instance));
Reset(instance);
lock (builders)
{
builders.Push(builder);
builders.Push(instance);
}
}
protected virtual void Reset(T builder)
/// <summary>
/// Resets the specified instance when <see cref="Release"/> is called before storing back to this cache.
/// </summary>
/// <param name="instance">The instance.</param>
protected virtual void Reset(T instance)
{
}
}

View File

@@ -6,6 +6,12 @@ using System.Collections.Generic;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// A List that provides methods for inserting/finding before/after. See remarks.
/// </summary>
/// <typeparam name="T">Type of the list item</typeparam>
/// <seealso cref="System.Collections.Generic.List{T}" />
/// <remarks>We use a typed list and don't use extension methods because it would pollute all list implemts and the top level namespace.</remarks>
public class OrderedList<T> : List<T>
{
public bool InsertBefore<TElement>(T element) where TElement : T

View File

@@ -6,6 +6,10 @@ using System.Text;
namespace Textamina.Markdig.Helpers
{
/// <summary>
/// An implementation of <see cref="ObjectCache{T}"/> for <see cref="StringBuilder"/>
/// </summary>
/// <seealso cref="Textamina.Markdig.Helpers.ObjectCache{System.Text.StringBuilder}" />
public class StringBuilderCache : ObjectCache<StringBuilder>
{
/// <summary>
@@ -14,6 +18,10 @@ namespace Textamina.Markdig.Helpers
[ThreadStatic]
private static StringBuilder local;
/// <summary>
/// Provides a string builder that can only be used locally in a method. This StringBuilder MUST not be stored.
/// </summary>
/// <returns></returns>
public static StringBuilder Local()
{
var sb = local ?? (local = new StringBuilder());
@@ -24,11 +32,11 @@ namespace Textamina.Markdig.Helpers
return sb;
}
protected override void Reset(StringBuilder builder)
protected override void Reset(StringBuilder instance)
{
if (builder.Length > 0)
if (instance.Length > 0)
{
builder.Clear();
instance.Clear();
}
}
}

View File

@@ -10,7 +10,7 @@ using Textamina.Markdig.Syntax;
namespace Textamina.Markdig
{
/// <summary>
/// Provides methods for parsing to a syntax tree and converting Markdown to other formats.
/// Provides methods for parsing a Markdown string to a syntax tree and converting it to other formats.
/// </summary>
public class Markdown
{
@@ -55,6 +55,8 @@ namespace Textamina.Markdig
if (reader == null) throw new ArgumentNullException(nameof(reader));
if (writer == null) throw new ArgumentNullException(nameof(writer));
pipeline = pipeline ?? new MarkdownPipeline();
// We override the renderer with our own writer
pipeline.Renderer = new HtmlRenderer(writer);
var document = Parse(reader, pipeline);
@@ -62,6 +64,25 @@ namespace Textamina.Markdig
writer.Flush();
}
/// <summary>
/// Converts a Markdown string using a custom <see cref="IMarkdownRenderer"/> specified in the <see cref="MarkdownPipeline.Renderer"/>.
/// </summary>
/// <param name="reader">A Markdown text from a <see cref="TextReader"/>.</param>
/// <param name="pipeline">The pipeline used for the conversion.</param>
/// <exception cref="System.ArgumentNullException">if reader or writer variable are null</exception>
public static object Convert(TextReader reader, MarkdownPipeline pipeline = null)
{
if (reader == null) throw new ArgumentNullException(nameof(reader));
pipeline = pipeline ?? new MarkdownPipeline();
if (pipeline.Renderer == null)
{
throw new InvalidOperationException("The property MarkdownPipeline.Renderer cannot be null");
}
var document = Parse(reader, pipeline);
return pipeline.Renderer.Render(document);
}
/// <summary>
/// Parses the specified markdown into an AST <see cref="Document"/>
/// </summary>

View File

@@ -9,7 +9,7 @@ using Textamina.Markdig.Extensions.Tables;
namespace Textamina.Markdig
{
/// <summary>
/// Provides extension methods for <see cref="MarkdownPipeline"/> to enable extensions.
/// Provides extension methods for <see cref="MarkdownPipeline"/> to enable several Markdown extensions.
/// </summary>
public static class MarkdownExtensions
{