2019-08-19 12:35:39 +00:00
|
|
|
using System;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Textile.States
|
|
|
|
{
|
|
|
|
public class TableCellParser
|
2020-08-27 14:01:37 +00:00
|
|
|
{
|
2019-08-19 12:35:39 +00:00
|
|
|
readonly string m_lineFragment;
|
|
|
|
|
2020-08-27 14:01:37 +00:00
|
|
|
public TableCellParser(string input)
|
|
|
|
{
|
|
|
|
m_lineFragment = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetLineFragmentFormatting()
|
|
|
|
{
|
|
|
|
var htmlTag = "td";
|
|
|
|
|
|
|
|
var m = Regex.Match(m_lineFragment,
|
|
|
|
@"^((?<head>_?)" +
|
|
|
|
Globals.SpanPattern +
|
|
|
|
Globals.AlignPattern +
|
|
|
|
Globals.BlockModifiersPattern +
|
|
|
|
@"(?<dot>\.)\s?)?" +
|
|
|
|
@"(?<content>.*)"
|
|
|
|
);
|
|
|
|
if (!m.Success)
|
|
|
|
throw new Exception("Couldn't parse table cell.");
|
|
|
|
|
|
|
|
if (m.Groups["head"].Value == "_")
|
|
|
|
htmlTag = "th";
|
2019-08-19 12:35:39 +00:00
|
|
|
//string opts = BlockAttributesParser.ParseBlockAttributes(m.Groups["span"].Value, "td") +
|
|
|
|
// BlockAttributesParser.ParseBlockAttributes(m.Groups["align"].Value, "td") +
|
|
|
|
// BlockAttributesParser.ParseBlockAttributes(m.Groups["atts"].Value, "td");
|
|
|
|
var opts = Blocks.BlockAttributesParser.ParseBlockAttributes(m.Groups["span"].Value + m.Groups["align"].Value + m.Groups["atts"].Value, "td");
|
|
|
|
|
2020-08-27 14:01:37 +00:00
|
|
|
var res = "<" + htmlTag + opts + ">";
|
2019-08-19 12:35:39 +00:00
|
|
|
// It may be possible the user actually intended to have a dot at the beginning of
|
|
|
|
// this cell's text, without any formatting (header tag or options).
|
|
|
|
if (string.IsNullOrEmpty(opts) && htmlTag == "td" && !string.IsNullOrEmpty(m.Groups["dot"].Value))
|
|
|
|
res += ".";
|
2020-08-27 14:01:37 +00:00
|
|
|
res += m.Groups["content"].Value;
|
|
|
|
res += "</" + htmlTag + ">";
|
2019-08-19 12:35:39 +00:00
|
|
|
|
2020-08-27 14:01:37 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 12:35:39 +00:00
|
|
|
}
|