Пример #1
0
		public static TableBlock TryParse(CreoleReader reader)
		{
			if (reader.Peek(1) != "|")
				return null;

			bool isFirstLine = true;
			string[] headers = null;
			List<string[]> rows = new List<string[]>();
			while (!reader.EndOfMarkup)
			{
				var line = reader.PeekLine().TrimEnd();

				if (!line.StartsWith("|"))
					break;

				var split = line.Substring(1).Split(new char[] { '|' }, StringSplitOptions.None);
				if (string.IsNullOrEmpty(split.Last().Trim()))
					split = split.Take(split.Count() - 1).ToArray();

				if (isFirstLine && !split.Any(p => !p.StartsWith("=")))
					headers = split.Select(p => p.Substring(1)).ToArray();
				else
					rows.Add(split);

				reader.ReadLine();
				isFirstLine = false;
			}

			return new TableBlock(headers, rows.ToArray());
		}
Пример #2
0
		private void RenderList(CreoleParser parser, CreoleWriter writer, CreoleReader reader, int level)
		{
			var indent = "";
			for (int i = 1; i < level * 2 - 1; i++)
				indent += "    ";

			while (!reader.EndOfMarkup)
			{
				var type = reader.Peek(level);
				if (type.Any(c => c != type[0]))
					break;
				type = type[0].ToString();

				if (type == "*") writer.AppendRawLine("{0}<ul>", indent);
				else if (type == "#") writer.AppendRawLine("{0}<ol>", indent);
				else throw new InvalidOperationException("Invalid list type");

				while (!reader.EndOfMarkup)
				{
					var nextType = reader.Peek(level);
					if (nextType.Any(c => c != type[0]))
						break;

					reader.Skip(level);
					var line = reader.ReadLine();
					var indentEndLi = false;

					writer.AppendRaw("{0}    <li>", indent);
					writer.AppendRaw(parser.ParseInlines(line).Trim());
					if (!reader.EndOfMarkup)
					{
						var next = reader.Peek(level + 1);
						if (next.Length == level + 1 && (!next.Any(c => c != '*') || !next.Any(c => c != '#')))
						{
							writer.AppendRawLine("");
							RenderList(parser, writer, reader, level + 1);
							indentEndLi = true;
						}
					}
					if (indentEndLi)
						writer.AppendRawLine("{0}    </li>", indent);
					else
						writer.AppendRawLine("</li>");
				}

				if (type == "*") writer.AppendRawLine("{0}</ul>", indent);
				else if (type == "#") writer.AppendRawLine("{0}</ol>", indent);
				else throw new InvalidOperationException("Invalid list type");
			}
		}