Exemplo n.º 1
0
        public static List <TableRowToken> TryParseRows(Pullenti.Ner.Token t, int maxChar, bool mustBeStartOfTable)
        {
            if (t == null)
            {
                return(null);
            }
            bool isTab = false;

            if (mustBeStartOfTable)
            {
                if (!t.IsChar((char)0x1E))
                {
                    return(null);
                }
                isTab = true;
            }
            TableRowToken rw = Parse(t, maxChar, null, ref isTab);

            if (rw == null)
            {
                return(null);
            }
            List <TableRowToken> res = new List <TableRowToken>();

            res.Add(rw);
            for (t = rw.EndToken.Next; t != null; t = t.Next)
            {
                TableRowToken rw0 = Parse(t, maxChar, rw, ref isTab);
                if (rw0 == null)
                {
                    break;
                }
                res.Add((rw = rw0));
                t = rw0.EndToken;
                if (rw0.LastRow)
                {
                    break;
                }
            }
            TableRowToken rla = res[res.Count - 1];

            if (((rla.LastRow && rla.Cells.Count == 2 && rla.Cells[0].ColSpan == 1) && rla.Cells[0].RowSpan == 1 && rla.Cells[1].ColSpan == 1) && rla.Cells[1].RowSpan == 1)
            {
                List <TableCellToken> lines0 = rla.Cells[0].Lines;
                List <TableCellToken> lines1 = rla.Cells[1].Lines;
                if (lines0.Count > 2 && lines1.Count == lines0.Count)
                {
                    for (int ii = 0; ii < lines0.Count; ii++)
                    {
                        rw = new TableRowToken((ii == 0 ? lines0[ii].BeginToken : lines1[ii].BeginToken), (ii == 0 ? lines0[ii].EndToken : lines1[ii].EndToken));
                        rw.Cells.Add(lines0[ii]);
                        rw.Cells.Add(lines1[ii]);
                        rw.Eor = rla.Eor;
                        if (ii == (lines0.Count - 1))
                        {
                            rw.LastRow  = rla.LastRow;
                            rw.EndToken = rla.EndToken;
                        }
                        res.Add(rw);
                    }
                    res.Remove(rla);
                }
            }
            foreach (TableRowToken re in res)
            {
                if (re.Cells.Count > 1)
                {
                    return(res);
                }
                if (re.Cells.Count == 1)
                {
                    if (_containsTableChar(re.Cells[0]))
                    {
                        return(res);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        static TableRowToken Parse(Pullenti.Ner.Token t, int maxChar, TableRowToken prev, ref bool isTab)
        {
            if (t == null || ((t.EndChar > maxChar && maxChar > 0)))
            {
                return(null);
            }
            string txt = t.Kit.Sofa.Text;

            Pullenti.Ner.Token t0 = t;
            if (t.IsChar((char)0x1E) && t.Next != null)
            {
                isTab = true;
                t     = t.Next;
            }
            Pullenti.Ner.Token tt;
            TableInfo          cellInfo = null;

            for (tt = t; tt != null && ((tt.EndChar <= maxChar || maxChar == 0)); tt = tt.Next)
            {
                if (tt.IsTableControlChar)
                {
                    cellInfo = new TableInfo(tt);
                    if (cellInfo.Typ != TableTypes.CellEnd)
                    {
                        cellInfo = null;
                    }
                    break;
                }
                else if (tt.IsNewlineAfter)
                {
                    if (!isTab && prev == null)
                    {
                        break;
                    }
                    if ((tt.EndChar - t.BeginChar) > 100)
                    {
                        if ((tt.EndChar - t.BeginChar) > 10000)
                        {
                            break;
                        }
                        if (!isTab)
                        {
                            break;
                        }
                    }
                    if (tt.WhitespacesAfterCount > 15)
                    {
                        if (!isTab)
                        {
                            break;
                        }
                    }
                }
            }
            if (cellInfo == null)
            {
                return(null);
            }
            TableRowToken res = new TableRowToken(t0, tt);

            res.Cells.Add(new TableCellToken(t, tt)
            {
                RowSpan = cellInfo.RowSpan, ColSpan = cellInfo.ColSpan
            });
            for (tt = tt.Next; tt != null && ((tt.EndChar <= maxChar || maxChar == 0)); tt = tt.Next)
            {
                t0       = tt;
                cellInfo = null;
                for (; tt != null && ((tt.EndChar <= maxChar || maxChar == 0)); tt = tt.Next)
                {
                    if (tt.IsTableControlChar)
                    {
                        cellInfo = new TableInfo(tt);
                        break;
                    }
                    else if (tt.IsNewlineAfter)
                    {
                        if (!isTab && prev == null)
                        {
                            break;
                        }
                        if ((tt.EndChar - t0.BeginChar) > 400)
                        {
                            if ((tt.EndChar - t0.BeginChar) > 20000)
                            {
                                break;
                            }
                            if (!isTab)
                            {
                                break;
                            }
                        }
                        if (tt.WhitespacesAfterCount > 15)
                        {
                            if (!isTab)
                            {
                                break;
                            }
                        }
                    }
                }
                if (cellInfo == null)
                {
                    break;
                }
                if (cellInfo.Typ == TableTypes.RowEnd)
                {
                    if (tt != t0)
                    {
                        res.Cells.Add(new TableCellToken(t0, tt)
                        {
                            RowSpan = cellInfo.RowSpan, ColSpan = cellInfo.ColSpan
                        });
                    }
                    res.EndToken = tt;
                    res.Eor      = true;
                    break;
                }
                if (cellInfo.Typ != TableTypes.CellEnd)
                {
                    break;
                }
                res.Cells.Add(new TableCellToken(t0, tt)
                {
                    RowSpan = cellInfo.RowSpan, ColSpan = cellInfo.ColSpan
                });
                res.EndToken = tt;
            }
            if ((res.Cells.Count < 2) && !res.Eor)
            {
                return(null);
            }
            if (res.EndToken.Next != null && res.EndToken.Next.IsChar((char)0x1F))
            {
                res.LastRow  = true;
                res.EndToken = res.EndToken.Next;
            }
            return(res);
        }