0

confluence wiki v6.0中有两种不同的表类型。

所以我正在努力通过第一行来确定表类型(我通过像这样的正则表达式检测新行new Regex(@"(\|(\r\n|\r|\n)(.*?)\|)+");并使用拆分来拆分行Matches,但是)

表格行可能如下所示:

如果它的标题

|| 标题 1 || 标题 2 || 标题 3 ||

如果它的常规行

| 单元格 A1 | 单元格 A2 | 单元格 A3 |

如果它的垂直表格行

||标题 |单元格 B2 | 单元格 B3 |

我尝试使用这样的表达式,^(\|\|.*?\|)但发现它也适用于标题。

由于标题标记功能,我尝试使用此^(\|\|.*?\|\|)功能后,但如果它是常规行,这无济于事

那么是否有可能实现行类型的确定,或者至少可以说它是使用的垂直行Regex吗?

或者最好写一些可以逐步处理行的东西?

4

1 回答 1

0

没有使用正则表达式和 in 就写了javascript,看起来像这样

简单的字符串扫描器

var Scanner = (function(){
    function Scanner(text){
        this.currentString = text.split('');
        this.position = 0;
        this.errorList = [];
        this.getChar = function(){
            var me = this,
                pos = me.position,
                string = me.currentString,
                stringLength = string.length;

            if(pos < stringLength){
                return string[pos];
            }

            return -1;
        };

        this.nextChar = function(){
            var me = this,
                pos = me.position,
                string = me.currentString,
                stringLength = string.length;

            if(pos < stringLength){
                me.position++;
                return;
            }

            me.error("EOL reached");
        };

        this.error = function(errorMsg){
            var me = this,
                error = "Error at position " + me.position +"\nMessage: "+errorMsg+".\n";
                errors = me.errorList;

            errors.push[error];
        };      

        return this;
    };

    return Scanner;

})();

简单的解析器

 /**
     LINE ::= { CELL }

     CELL ::= '|' CELL1
     CELL1 ::= HEADER_CELL | REGULAR_CELL

     HEADER_CELL ::=  '|'  TEXT
     REGULAR_CELL ::=  TEXT

 */

 function RowParser(){
    this.scanner = {}; 
    this.rawText = "";
    this.cellsData = [];

    return this;
};

RowParser.prototype = {
    parseRow: function(row){
        var me = this;

        me.scanner = new Scanner(row);
        me.rawText = row;
        me.cellsData = [];

        me.proceedNext();
    },

    proceedNext: function(){
        var me = this,
            scanner = me.scanner;

        while(scanner.getChar() === '|'){
            me.proceedCell();
        }

        if (scanner.getChar() !== -1)
        {
            scanner.error("EOL expected, "+ scanner.getChar() +" got");
        }

        return;
    },

    proceedCell: function(){
        var me = this,
            scanner = me.scanner;

        if(scanner.getChar() === '|'){
            scanner.nextChar();
            me.proceedHeaderCell();
        }
    },

    proceedHeaderCell: function(){
        var me = this,
            scanner = me.scanner;

        if(scanner.getChar() === '|'){
            me.onHeaderCell();
        } else { 
            me.onRegularCell();
        }
    },

    onHeaderCell: function(){
        var me = this,
            scanner = me.scanner,
            cellType = TableCellType.info,
            cellData = {
                type: cellType.Header
            }

        if(scanner.getChar() === '|'){
            scanner.nextChar();
            me.proceedInnerText(cellType.Header);
        }else{
            scanner.error("Expected '|' got "+ currentChar +".");
        }           
    },

    onRegularCell:function(){
        var me = this,
            scanner = me.scanner,
            cellType = TableCellType.info;

        me.proceedInnerText(cellType.Regular);  
    },  

    proceedInnerText: function(cellType){
        var me = this,
            scanner = me.scanner,
            typeData = TableCellType.getValueById(cellType),
            innerText = [];

        while(scanner.getChar() !== '|' && scanner.getChar() !== -1){
            innerText.push(scanner.getChar());
            scanner.nextChar();
        }           

        me.cellsData.push({
            typeId: typeData.id,
            type: typeData.name,
            text: innerText.join("")
        });

        me.proceedNext();       
    },

    getRowData: function(){
        var me = this,
            scanner = me.scanner,
            data = me.cellsData,
            emptyCell;

        //Proceed cell data
        //if there no empty cell in the end - means no close tag
        var filteredData = data.filter(function(el){
            return el.text.length !== 0;
        });

        if(filteredData.length === data.length){
            scanner.error("No close tag at row "+ me.rawText +".");
            return;
        }           

        for (var i = 0; i < filteredData.length; i++) {
            filteredData[i].text = filteredData[i].text.trim();
        }

        return filteredData;
    }
};

CellTypeEnum上文提到的

var TableCellType = {
    info:{
        Regular: 10,
        Header: 20
    },

    data:[
        {
            id: 10,
            name: "regular"
        },
        {
            id: 20,
            name: "header"
        }
    ],

    getValueById: function(id){
        var me = this,
            data = me.data,
            result = data.filter(function(el){
                return el.id === id;
            });

        return result[0];   
    }       
}

用法:

var rowParser = new RowParser();
var row = "||AAA||BBB||CCC||\n|Hi|all|people!|";
rowParser.parseRow(row);
var result = rowParser.getRowData();
于 2017-01-31T14:28:05.273 回答