Exemple #1
0
        /// <summary>
        /// Inserts the specified inline after this instance.
        /// </summary>
        /// <param name="next">The inline to insert after this instance.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException">Inline has already a parent</exception>
        public void InsertAfter(Inline next)
        {
            if (next == null)
            {
                ThrowHelper.ArgumentNullException(nameof(next));
            }
            if (next.Parent != null)
            {
                ThrowHelper.ArgumentException("Inline has already a parent", nameof(next));
            }

            var previousNext = NextSibling;

            if (previousNext != null)
            {
                previousNext.PreviousSibling = next;
            }

            next.PreviousSibling = this;
            next.NextSibling     = previousNext;
            NextSibling          = next;

            if (Parent != null)
            {
                Parent.OnChildInsert(next);
                next.Parent = Parent;
            }
        }
        public static IntPtr ImportMethod(IntPtr moduleHandle, string methodName)
        {
            if (moduleHandle == IntPtr.Zero)
            {
                throw ThrowHelper.ArgumentOutOfRangeException(nameof(moduleHandle));
            }

            if (methodName == null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(methodName));
            }
            if (methodName.Length == 0)
            {
                throw ThrowHelper.ArgumentOutOfRangeException(nameof(methodName));
            }

            IntPtr procAddress = GetProcAddress(moduleHandle, methodName);

            if (procAddress == IntPtr.Zero)
            {
                throw new DynamicImportException($"DynamicImport failed to find method \"{ methodName }\" in module \"0x{ moduleHandle.ToString("X") }\"!");
            }
            else
            {
                return(procAddress);
            }
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MarkdownParser" /> class.
        /// </summary>
        /// <param name="text">The reader.</param>
        /// <param name="pipeline">The pipeline.</param>
        /// <param name="context">A parser context used for the parsing.</param>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        private MarkdownParser(string text, MarkdownPipeline pipeline, MarkdownParserContext context)
        {
            if (text == null)
            {
                ThrowHelper.ArgumentNullException_text();
            }
            if (pipeline == null)
            {
                ThrowHelper.ArgumentNullException(nameof(pipeline));
            }

            roughLineCountEstimate = text.Length / 40;
            text                  = FixupZero(text);
            lineReader            = new LineReader(text);
            preciseSourceLocation = pipeline.PreciseSourceLocation;

            // Initialize the pipeline
            document = new MarkdownDocument();

            // Initialize the block parsers
            blockProcessor = new BlockProcessor(document, pipeline.BlockParsers, context);

            // Initialize the inline parsers
            inlineProcessor = new InlineProcessor(document, pipeline.InlineParsers, pipeline.PreciseSourceLocation, context)
            {
                DebugLog = pipeline.DebugLog
            };

            documentProcessed = pipeline.DocumentProcessed;
        }
Exemple #4
0
        /// <summary>
        /// Creates a pipeline automatically configured from an input markdown based on the presence of the configuration tag.
        /// </summary>
        /// <param name="inputText">The input text.</param>
        /// <returns>The pipeline configured from the input</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public MarkdownPipeline CreatePipelineFromInput(string inputText)
        {
            if (inputText == null)
            {
                ThrowHelper.ArgumentNullException(nameof(inputText));
            }

            var    builder             = new MarkdownPipelineBuilder();
            string defaultConfig       = DefaultExtensions;
            var    indexOfSelfPipeline = inputText.IndexOf(SelfPipelineHintTagStart, StringComparison.OrdinalIgnoreCase);

            if (indexOfSelfPipeline >= 0)
            {
                var optionStart = indexOfSelfPipeline + SelfPipelineHintTagStart.Length;
                var endOfTag    = inputText.IndexOf("-->", optionStart, StringComparison.OrdinalIgnoreCase);
                if (endOfTag >= 0)
                {
                    defaultConfig = inputText.Substring(optionStart, endOfTag - optionStart).Trim();
                }
            }

            if (!string.IsNullOrEmpty(defaultConfig))
            {
                builder.Configure(defaultConfig);
            }
            return(builder.Build());
        }
Exemple #5
0
        /// <summary>
        /// Inserts the specified inline before this instance.
        /// </summary>
        /// <param name="previous">The inline previous to insert before this instance.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException">Inline has already a parent</exception>
        public void InsertBefore(Inline previous)
        {
            if (previous == null)
            {
                ThrowHelper.ArgumentNullException(nameof(previous));
            }
            if (previous.Parent != null)
            {
                ThrowHelper.ArgumentException("Inline has already a parent", nameof(previous));
            }

            var previousSibling = PreviousSibling;

            if (previousSibling != null)
            {
                previousSibling.NextSibling = previous;
            }

            PreviousSibling      = previous;
            previous.NextSibling = this;

            if (Parent != null)
            {
                Parent.OnChildInsert(previous);
                previous.Parent = Parent;
            }
        }
        public static IntPtr ImportLibrary(string libraryName)
        {
            if (libraryName == null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(libraryName));
            }
            if (libraryName.Length == 0)
            {
                throw ThrowHelper.ArgumentOutOfRangeException(nameof(libraryName));
            }

            IntPtr hModule = GetModuleHandle(libraryName);

            if (hModule == IntPtr.Zero)
            {
                hModule = LoadLibrary(libraryName);
            }

            if (hModule == IntPtr.Zero)
            {
                throw new DynamicImportException($"DynamicImport failed to import library \"{ libraryName }\"!");
            }
            else
            {
                return(hModule);
            }
        }
Exemple #7
0
 public void Sort(Comparison <Block> comparison)
 {
     if (comparison == null)
     {
         ThrowHelper.ArgumentNullException(nameof(comparison));
     }
     Array.Sort(children, 0, Count, new BlockComparer(comparison));
 }
Exemple #8
0
 public void Sort(IComparer <Block> comparer)
 {
     if (comparer == null)
     {
         ThrowHelper.ArgumentNullException(nameof(comparer));
     }
     Array.Sort(children, 0, Count, comparer);
 }
Exemple #9
0
 public void Sort(IComparer <Block> comparer)
 {
     if (comparer is null)
     {
         ThrowHelper.ArgumentNullException(nameof(comparer));
     }
     Array.Sort(_children, 0, Count, new BlockComparerWrapper(comparer));
 }
Exemple #10
0
 protected DelimiterInline(InlineParser parser)
 {
     if (parser == null)
     {
         ThrowHelper.ArgumentNullException(nameof(parser));
     }
     Parser   = parser;
     IsActive = true;
 }
Exemple #11
0
 public Enumerator(ContainerInline container) : this()
 {
     if (container == null)
     {
         ThrowHelper.ArgumentNullException(nameof(container));
     }
     this.container = container;
     currentChild   = nextChild = container.FirstChild;
 }
Exemple #12
0
        /// <summary>
        /// Sets all pins to low level.
        /// </summary>
        /// <param name="expander">Object of type IGpioExpander.</param>
        /// <exception cref="ArgumentNullException">Object of type IGpioExpander is null.</exception>
        public static void DigitalPortLowLevel(this GpioExpander expander)
        {
            if (expander is null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(expander));
            }

            expander.DigitalWritePort(0);
        }
Exemple #13
0
        /// <summary>
        /// Fixes click of the TroykaButton.
        /// </summary>
        /// <param name="expander">Object of type IGpioExpander.</param>
        /// <param name="pin">Pin number.</param>
        /// <returns>True - click, False - not click.</returns>
        /// <exception cref="ArgumentNullException">Object of type IGpioExpander is null.</exception>
        public static bool TroykaButtonClick(this GpioExpander expander, int pin)
        {
            if (expander is null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(expander));
            }

            return(!expander.DigitalRead(pin));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EmphasisDelimiterInline" /> class.
        /// </summary>
        /// <param name="parser">The parser.</param>
        /// <param name="descriptor">The descriptor.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public EmphasisDelimiterInline(InlineParser parser, EmphasisDescriptor descriptor) : base(parser)
        {
            if (descriptor is null)
            {
                ThrowHelper.ArgumentNullException(nameof(descriptor));
            }

            Descriptor    = descriptor;
            DelimiterChar = descriptor.Character;
        }
Exemple #15
0
 /// <summary>
 /// Allows to setup a <see cref="IMarkdownRenderer"/>.
 /// </summary>
 /// <param name="renderer">The markdown renderer to setup</param>
 public void Setup(IMarkdownRenderer renderer)
 {
     if (renderer is null)
     {
         ThrowHelper.ArgumentNullException(nameof(renderer));
     }
     foreach (var extension in Extensions)
     {
         extension.Setup(this, renderer);
     }
 }
Exemple #16
0
 /// <summary>
 /// Opens the specified block.
 /// </summary>
 /// <param name="block">The block.</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="ArgumentException">The block must be opened</exception>
 public void Open(Block block)
 {
     if (block == null)
     {
         ThrowHelper.ArgumentNullException(nameof(block));
     }
     if (!block.IsOpen)
     {
         ThrowHelper.ArgumentException("The block must be opened", nameof(block));
     }
     OpenedBlocks.Add(block);
 }
Exemple #17
0
        /// <summary>
        /// Create a <see cref="IHostProvider"/> with delegate handler.
        /// </summary>
        /// <param name="hostPrefix">Prefix of host that can be handled.</param>
        /// <param name="handler">Handler that generate iframe url, if uri cannot be handled, it can return <see langword="null"/>.</param>
        /// <param name="allowFullScreen">Should the generated iframe has allowfullscreen attribute.</param>
        /// <param name="iframeClass">"class" attribute of generated iframe.</param>
        /// <returns>A <see cref="IHostProvider"/> with delegate handler.</returns>
        public static IHostProvider Create(string hostPrefix, Func <Uri, string?> handler, bool allowFullScreen = true, string?iframeClass = null)
        {
            if (string.IsNullOrEmpty(hostPrefix))
            {
                ThrowHelper.ArgumentException("hostPrefix is null or empty.", nameof(hostPrefix));
            }
            if (handler is null)
            {
                ThrowHelper.ArgumentNullException(nameof(handler));
            }

            return(new DelegateProvider(hostPrefix, handler, allowFullScreen, iframeClass));
        }
Exemple #18
0
        public static GpioExpander GetGpioExpander(this I2cBus bus, int address = GpioExpander.DefaultAddress)
        {
            if (bus is null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(bus));
            }

            if (address is < 0 or > 127)
            {
                throw ThrowHelper.ArgumentOutOfRangeException(nameof(address), address);
            }

            return(new RaspberryPiExpander(bus.CreateDevice(address)));
        }
 public void Set(string label, LinkReferenceDefinition link)
 {
     if (link == null)
     {
         ThrowHelper.ArgumentNullException(nameof(link));
     }
     if (!Contains(link))
     {
         Add(link);
         if (!Links.ContainsKey(label))
         {
             Links[label] = link;
         }
     }
 }
Exemple #20
0
        // This class is immutable

        /// <summary>
        /// Initializes a new instance of the <see cref="MarkdownPipeline" /> class.
        /// </summary>
        internal MarkdownPipeline(OrderedList <IMarkdownExtension> extensions, BlockParserList blockParsers, InlineParserList inlineParsers, TextWriter debugLog, ProcessDocumentDelegate documentProcessed)
        {
            if (blockParsers == null)
            {
                ThrowHelper.ArgumentNullException(nameof(blockParsers));
            }
            if (inlineParsers == null)
            {
                ThrowHelper.ArgumentNullException(nameof(inlineParsers));
            }
            // Add all default parsers
            Extensions        = extensions;
            BlockParsers      = blockParsers;
            InlineParsers     = inlineParsers;
            DebugLog          = debugLog;
            DocumentProcessed = documentProcessed;
        }
Exemple #21
0
        /// <summary>
        /// Converts a Markdown document to HTML.
        /// </summary>
        /// <param name="document">A Markdown document.</param>
        /// <param name="pipeline">The pipeline used for the conversion.</param>
        /// <returns>The result of the conversion</returns>
        /// <exception cref="ArgumentNullException">if markdown document variable is null</exception>
        public static string ToHtml(this MarkdownDocument document, MarkdownPipeline?pipeline = null)
        {
            if (document is null)
            {
                ThrowHelper.ArgumentNullException(nameof(document));
            }

            pipeline ??= DefaultPipeline;

            using var rentedRenderer = pipeline.RentHtmlRenderer();
            HtmlRenderer renderer = rentedRenderer.Instance;

            renderer.Render(document);
            renderer.Writer.Flush();

            return(renderer.Writer.ToString() ?? string.Empty);
        }
Exemple #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InlineProcessor" /> class.
 /// </summary>
 /// <param name="document">The document.</param>
 /// <param name="parsers">The parsers.</param>
 /// <param name="preciseSourcelocation">A value indicating whether to provide precise source location.</param>
 /// <param name="context">A parser context used for the parsing.</param>
 /// <exception cref="ArgumentNullException">
 /// </exception>
 public InlineProcessor(MarkdownDocument document, InlineParserList parsers, bool preciseSourcelocation, MarkdownParserContext context)
 {
     if (document == null)
     {
         ThrowHelper.ArgumentNullException(nameof(document));
     }
     if (parsers == null)
     {
         ThrowHelper.ArgumentNullException(nameof(parsers));
     }
     Document = document;
     Parsers  = parsers;
     Context  = context;
     PreciseSourceLocation = preciseSourcelocation;
     lineOffsets           = new List <StringLineGroup.LineOffset>();
     ParserStates          = new object[Parsers.Count];
     LiteralInlineParser   = new LiteralInlineParser();
 }
Exemple #23
0
        /// <summary>
        /// Converts a Markdown string using a custom <see cref="IMarkdownRenderer"/>.
        /// </summary>
        /// <param name="markdown">A Markdown text.</param>
        /// <param name="renderer">The renderer to convert Markdown to.</param>
        /// <param name="pipeline">The pipeline used for the conversion.</param>
        /// <param name="context">A parser context used for the parsing.</param>
        /// <exception cref="ArgumentNullException">if markdown or writer variable are null</exception>
        public static object Convert(string markdown, IMarkdownRenderer renderer, MarkdownPipeline?pipeline = null, MarkdownParserContext?context = null)
        {
            if (markdown is null)
            {
                ThrowHelper.ArgumentNullException_markdown();
            }
            if (renderer is null)
            {
                ThrowHelper.ArgumentNullException(nameof(renderer));
            }

            pipeline = GetPipeline(pipeline, markdown);

            var document = MarkdownParser.Parse(markdown, pipeline, context);

            pipeline.Setup(renderer);
            return(renderer.Render(document));
        }
Exemple #24
0
        /// <summary>
        /// Embraces this instance by the specified container.
        /// </summary>
        /// <param name="container">The container to use to embrace this instance.</param>
        /// <exception cref="ArgumentNullException">If the container is null</exception>
        public void EmbraceChildrenBy(ContainerInline container)
        {
            if (container == null)
            {
                ThrowHelper.ArgumentNullException(nameof(container));
            }
            var child = FirstChild;

            while (child != null)
            {
                var next = child.NextSibling;
                // TODO: optimize this
                child.Remove();
                container.AppendChild(child);
                child = next;
            }
            AppendChild(container);
        }
Exemple #25
0
        /// <summary>
        /// Converts a Markdown string using a custom <see cref="IMarkdownRenderer"/>.
        /// </summary>
        /// <param name="markdown">A Markdown text.</param>
        /// <param name="renderer">The renderer to convert Markdown to.</param>
        /// <param name="pipeline">The pipeline used for the conversion.</param>
        /// <param name="context">A parser context used for the parsing.</param>
        /// <exception cref="System.ArgumentNullException">if markdown or writer variable are null</exception>
        public static object Convert(string markdown, IMarkdownRenderer renderer, MarkdownPipeline pipeline = null, MarkdownParserContext context = null)
        {
            if (markdown == null)
            {
                ThrowHelper.ArgumentNullException_markdown();
            }
            if (renderer == null)
            {
                ThrowHelper.ArgumentNullException(nameof(renderer));
            }
            pipeline ??= new MarkdownPipelineBuilder().Build();

            pipeline = CheckForSelfPipeline(pipeline, markdown);
            var document = Parse(markdown, pipeline, context);

            pipeline.Setup(renderer);
            return(renderer.Render(document));
        }
Exemple #26
0
        /// <summary>
        /// Converts a Markdown document to HTML.
        /// </summary>
        /// <param name="document">A Markdown document.</param>
        /// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
        /// <param name="pipeline">The pipeline used for the conversion.</param>
        /// <returns>The result of the conversion</returns>
        /// <exception cref="ArgumentNullException">if markdown document variable is null</exception>
        public static void ToHtml(this MarkdownDocument document, TextWriter writer, MarkdownPipeline?pipeline = null)
        {
            if (document is null)
            {
                ThrowHelper.ArgumentNullException(nameof(document));
            }
            if (writer is null)
            {
                ThrowHelper.ArgumentNullException_writer();
            }

            pipeline ??= DefaultPipeline;

            using var rentedRenderer = pipeline.RentHtmlRenderer(writer);
            HtmlRenderer renderer = rentedRenderer.Instance;

            renderer.Render(document);
            renderer.Writer.Flush();
        }
Exemple #27
0
        /// <summary>
        /// Moves all the children of this container after the specified inline.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public void MoveChildrenAfter(Inline parent)
        {
            if (parent == null)
            {
                ThrowHelper.ArgumentNullException(nameof(parent));
            }
            var child       = FirstChild;
            var nextSibling = parent;

            while (child != null)
            {
                var next = child.NextSibling;
                // TODO: optimize this
                child.Remove();
                nextSibling.InsertAfter(child);
                nextSibling = child;
                child       = next;
            }
        }
        /// <summary>
        /// Converts the string representation of a KeyStates enum value to a KeyStates enum value.
        /// </summary>
        /// <param name="value">The string representation of a KeyStates enum value.</param>
        /// <returns>A KeyStates enum value.</returns>
        public static KeyStates FromString(string value)
        {
            if (value == null)
            {
                throw ThrowHelper.ArgumentNullException(nameof(value));
            }
            if (value.Length == 0)
            {
                throw ThrowHelper.ArgumentOutOfRangeException(nameof(value));
            }

            if (Enum.TryParse <KeyStates>(value, true, out var result))
            {
                return(result);
            }
            else
            {
                throw ThrowHelper.EnumParseException <KeyStates>(value);
            }
        }
Exemple #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BlockProcessor" /> class.
 /// </summary>
 /// <param name="document">The document to build blocks into.</param>
 /// <param name="parsers">The list of parsers.</param>
 /// <param name="context">A parser context used for the parsing.</param>
 /// <exception cref="ArgumentNullException">
 /// </exception>
 public BlockProcessor(MarkdownDocument document, BlockParserList parsers, MarkdownParserContext context)
 {
     if (document == null)
     {
         ThrowHelper.ArgumentNullException(nameof(document));
     }
     if (parsers == null)
     {
         ThrowHelper.ArgumentNullException(nameof(parsers));
     }
     parserStateCache = new BlockParserStateCache(this);
     Document         = document;
     document.IsOpen  = true;
     Parsers          = parsers;
     Context          = context;
     OpenedBlocks     = new List <Block>();
     NewBlocks        = new Stack <Block>();
     root             = this;
     Open(document);
 }
Exemple #30
0
        /// <summary>
        /// Copies/merge the values from this instance to the specified <see cref="HtmlAttributes"/> instance.
        /// </summary>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <param name="mergeIdAndProperties">If set to <c>true</c> it will merge properties to the target htmlAttributes. Default is <c>false</c></param>
        /// <param name="shared">If set to <c>true</c> it will try to share Classes and Properties if destination don't have them, otherwise it will make a copy. Default is <c>true</c></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void CopyTo(HtmlAttributes htmlAttributes, bool mergeIdAndProperties = false, bool shared = true)
        {
            if (htmlAttributes == null)
            {
                ThrowHelper.ArgumentNullException(nameof(htmlAttributes));
            }
            // Add html htmlAttributes to the object
            if (!mergeIdAndProperties || Id != null)
            {
                htmlAttributes.Id = Id;
            }
            if (htmlAttributes.Classes == null)
            {
                htmlAttributes.Classes = shared ? Classes : Classes != null ? new List <string>(Classes) : null;
            }
            else if (Classes != null)
            {
                htmlAttributes.Classes.AddRange(Classes);
            }

            if (htmlAttributes.Properties == null)
            {
                htmlAttributes.Properties = shared ? Properties : Properties != null ? new List <KeyValuePair <string, string> >(Properties) : null;
            }
            else if (Properties != null)
            {
                if (mergeIdAndProperties)
                {
                    foreach (var prop in Properties)
                    {
                        htmlAttributes.AddPropertyIfNotExist(prop.Key, prop.Value);
                    }
                }
                else
                {
                    htmlAttributes.Properties.AddRange(Properties);
                }
            }
        }