diff --git a/src/Textamina.Markdig/Helpers/CharHelper.cs b/src/Textamina.Markdig/Helpers/CharHelper.cs index b99430d8..9b89fccd 100644 --- a/src/Textamina.Markdig/Helpers/CharHelper.cs +++ b/src/Textamina.Markdig/Helpers/CharHelper.cs @@ -7,6 +7,9 @@ using System.Runtime.CompilerServices; namespace Textamina.Markdig.Helpers { + /// + /// Helper class for handling characters. + /// public static class CharHelper { public const int TabSize = 4; diff --git a/src/Textamina.Markdig/Helpers/EntityHelper.cs b/src/Textamina.Markdig/Helpers/EntityHelper.cs index 4b291422..f6aa5a24 100644 --- a/src/Textamina.Markdig/Helpers/EntityHelper.cs +++ b/src/Textamina.Markdig/Helpers/EntityHelper.cs @@ -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 { + /// + /// Helper class to decode an entity. + /// public static class EntityHelper { /// @@ -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 ] /// /// Source: http://www.w3.org/html/wg/drafts/html/master/syntax.html#named-character-references /// - private static readonly Dictionary _entityMap = new Dictionary(2125, StringComparer.Ordinal) + private static readonly Dictionary EntityMap = new Dictionary(2125, StringComparer.Ordinal) { { "Aacute", "\u00C1" }, { "aacute", "\u00E1" }, diff --git a/src/Textamina.Markdig/Helpers/HtmlHelper.cs b/src/Textamina.Markdig/Helpers/HtmlHelper.cs index d245dcd9..83e198cb 100644 --- a/src/Textamina.Markdig/Helpers/HtmlHelper.cs +++ b/src/Textamina.Markdig/Helpers/HtmlHelper.cs @@ -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 { + /// + /// Helper to parse several HTML tags. + /// public static class HtmlHelper { private static readonly char[] SearchBackAndAmp = { '\\', '&' }; @@ -533,6 +564,7 @@ namespace Textamina.Markdig.Helpers /// 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); } diff --git a/src/Textamina.Markdig/Helpers/LinkHelper.cs b/src/Textamina.Markdig/Helpers/LinkHelper.cs index 3af5b5dc..f43d495d 100644 --- a/src/Textamina.Markdig/Helpers/LinkHelper.cs +++ b/src/Textamina.Markdig/Helpers/LinkHelper.cs @@ -6,6 +6,9 @@ using Textamina.Markdig.Syntax; namespace Textamina.Markdig.Helpers { + /// + /// Helpers to parse Markdown links. + /// public static class LinkHelper { public static bool TryParseAutolink(StringSlice text, out string link, out bool isEmail) diff --git a/src/Textamina.Markdig/Helpers/MethodImplOptionPortable.cs b/src/Textamina.Markdig/Helpers/MethodImplOptionPortable.cs index 754b152a..b0f9285a 100644 --- a/src/Textamina.Markdig/Helpers/MethodImplOptionPortable.cs +++ b/src/Textamina.Markdig/Helpers/MethodImplOptionPortable.cs @@ -5,6 +5,9 @@ using System.Runtime.CompilerServices; namespace Textamina.Markdig.Helpers { + /// + /// Internal helper to allow to declare a method using AggressiveInlining without being .NET 4.0+ + /// internal static class MethodImplOptionPortable { public const MethodImplOptions AggressiveInlining = (MethodImplOptions)256; diff --git a/src/Textamina.Markdig/Helpers/ObjectCache.cs b/src/Textamina.Markdig/Helpers/ObjectCache.cs index d47cf1f8..12e32afd 100644 --- a/src/Textamina.Markdig/Helpers/ObjectCache.cs +++ b/src/Textamina.Markdig/Helpers/ObjectCache.cs @@ -6,15 +6,26 @@ using System.Collections.Generic; namespace Textamina.Markdig.Helpers { + /// + /// A simple object recycling system. + /// + /// Type of the object to cache public class ObjectCache where T : class, new() { private readonly Stack builders; + /// + /// Initializes a new instance of the class. + /// public ObjectCache() { builders = new Stack(); } + /// + /// Gets a new instance. + /// + /// public T Get() { lock (builders) @@ -28,17 +39,26 @@ namespace Textamina.Markdig.Helpers return new T(); } - public void Release(T builder) + /// + /// Releases the specified instance. + /// + /// The instance. + /// if instance is null + 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) + /// + /// Resets the specified instance when is called before storing back to this cache. + /// + /// The instance. + protected virtual void Reset(T instance) { } } diff --git a/src/Textamina.Markdig/Helpers/OrderedList.cs b/src/Textamina.Markdig/Helpers/OrderedList.cs index ee309d07..99465d8c 100644 --- a/src/Textamina.Markdig/Helpers/OrderedList.cs +++ b/src/Textamina.Markdig/Helpers/OrderedList.cs @@ -6,6 +6,12 @@ using System.Collections.Generic; namespace Textamina.Markdig.Helpers { + /// + /// A List that provides methods for inserting/finding before/after. See remarks. + /// + /// Type of the list item + /// + /// We use a typed list and don't use extension methods because it would pollute all list implemts and the top level namespace. public class OrderedList : List { public bool InsertBefore(T element) where TElement : T diff --git a/src/Textamina.Markdig/Helpers/StringBuilderCache.cs b/src/Textamina.Markdig/Helpers/StringBuilderCache.cs index 028e3682..f5f1a79d 100644 --- a/src/Textamina.Markdig/Helpers/StringBuilderCache.cs +++ b/src/Textamina.Markdig/Helpers/StringBuilderCache.cs @@ -6,6 +6,10 @@ using System.Text; namespace Textamina.Markdig.Helpers { + /// + /// An implementation of for + /// + /// public class StringBuilderCache : ObjectCache { /// @@ -14,6 +18,10 @@ namespace Textamina.Markdig.Helpers [ThreadStatic] private static StringBuilder local; + /// + /// Provides a string builder that can only be used locally in a method. This StringBuilder MUST not be stored. + /// + /// 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(); } } } diff --git a/src/Textamina.Markdig/Markdown.cs b/src/Textamina.Markdig/Markdown.cs index 36e73346..92111758 100644 --- a/src/Textamina.Markdig/Markdown.cs +++ b/src/Textamina.Markdig/Markdown.cs @@ -10,7 +10,7 @@ using Textamina.Markdig.Syntax; namespace Textamina.Markdig { /// - /// 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. /// 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(); } + /// + /// Converts a Markdown string using a custom specified in the . + /// + /// A Markdown text from a . + /// The pipeline used for the conversion. + /// if reader or writer variable are null + 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); + } + /// /// Parses the specified markdown into an AST /// diff --git a/src/Textamina.Markdig/MarkdownExtensions.cs b/src/Textamina.Markdig/MarkdownExtensions.cs index b4670236..04d895d6 100644 --- a/src/Textamina.Markdig/MarkdownExtensions.cs +++ b/src/Textamina.Markdig/MarkdownExtensions.cs @@ -9,7 +9,7 @@ using Textamina.Markdig.Extensions.Tables; namespace Textamina.Markdig { /// - /// Provides extension methods for to enable extensions. + /// Provides extension methods for to enable several Markdown extensions. /// public static class MarkdownExtensions {