|
| 1 | +using Markdig.Helpers; |
| 2 | +using Markdig.Parsers; |
| 3 | +using Markdig.Parsers.Inlines; |
| 4 | +using Markdig.Syntax.Inlines; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Markus.MarkdigExtensions.AutoInclude |
| 12 | +{ |
| 13 | + internal class AutoIncludeParser : BlockParser |
| 14 | + { |
| 15 | + |
| 16 | + private readonly string _openingSequence = "@@[["; |
| 17 | + private readonly string _closingSequence = "]]"; |
| 18 | + |
| 19 | + private readonly char _openingCharacter = '@'; |
| 20 | + private readonly char _firstClosingCharacter = ']'; |
| 21 | + |
| 22 | + public AutoIncludeParser() { |
| 23 | + |
| 24 | + OpeningCharacters = new[] { _openingCharacter }; |
| 25 | + } |
| 26 | + |
| 27 | + public override BlockState TryOpen(BlockProcessor processor) |
| 28 | + { |
| 29 | + if (processor.IsCodeIndent) |
| 30 | + { |
| 31 | + return BlockState.None; |
| 32 | + } |
| 33 | + |
| 34 | + var sourceColumn = processor.Column; |
| 35 | + var line = processor.Line; |
| 36 | + var sourcePosition = line.Start; |
| 37 | + var c = processor.NextChar(); |
| 38 | + |
| 39 | + int index = 1; |
| 40 | + |
| 41 | + |
| 42 | + while(c != '\0' && index < _openingSequence.Length) |
| 43 | + { |
| 44 | + if (c != _openingSequence[index]) |
| 45 | + { |
| 46 | + processor.Column = sourceColumn; |
| 47 | + processor.Line = line; |
| 48 | + return BlockState.None; |
| 49 | + } |
| 50 | + index++; |
| 51 | + c = processor.NextChar(); |
| 52 | + } |
| 53 | + |
| 54 | + //Exit if invalid opening sequence |
| 55 | + if (index != _openingSequence.Length) |
| 56 | + { |
| 57 | + processor.Column = sourceColumn; |
| 58 | + processor.Line = line; |
| 59 | + return BlockState.None; |
| 60 | + } |
| 61 | + |
| 62 | + StringBuilder sb = new StringBuilder(); |
| 63 | + |
| 64 | + if (c != _firstClosingCharacter) |
| 65 | + { |
| 66 | + sb.Append(c); |
| 67 | + while (c != _firstClosingCharacter) |
| 68 | + { |
| 69 | + c = processor.NextChar(); |
| 70 | + } |
| 71 | + } |
| 72 | + index = 0; |
| 73 | + |
| 74 | + while(c != '\0' && index < _closingSequence.Length) |
| 75 | + { |
| 76 | + if (c != _closingSequence[index]) |
| 77 | + { |
| 78 | + processor.Column = sourceColumn; |
| 79 | + processor.Line = line; |
| 80 | + return BlockState.None; |
| 81 | + } |
| 82 | + index++; |
| 83 | + c = processor.NextChar(); |
| 84 | + } |
| 85 | + if (index != _closingSequence.Length) |
| 86 | + { |
| 87 | + processor.Column = sourceColumn; |
| 88 | + processor.Line = line; |
| 89 | + return BlockState.None; |
| 90 | + } |
| 91 | + |
| 92 | + var autoIncludeBlock = new AutoIncludeBlock(this) |
| 93 | + { |
| 94 | + Column = sourceColumn, |
| 95 | + Span = { Start = sourcePosition }, |
| 96 | + Filename = new StringSlice(sb.ToString()) |
| 97 | + }; |
| 98 | + processor.GoToColumn(sourceColumn + _openingSequence.Length + sb.Length + _closingSequence.Length + 1); |
| 99 | + processor.NewBlocks.Push(autoIncludeBlock); |
| 100 | + |
| 101 | + autoIncludeBlock.Span.End = processor.Line.End; |
| 102 | + |
| 103 | + return BlockState.Break; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments