Grammalecte  Check-in [df0f360e7e]

Overview
Comment:[core][js] text as object
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | core | webext
Files: files | file ages | folders
SHA3-256: df0f360e7e17806f2fcf11036ec23b4d3c85d116bbc3382d90dea748c79658d1
User & Date: olr on 2017-07-29 09:30:26
Other Links: branch diff | manifest | tags
Context
2017-07-29
10:22
[core][js] unused module check-in: 9db9979305 user: olr tags: core, webext
09:30
[core][js] text as object check-in: df0f360e7e user: olr tags: core, webext
09:25
[core][js] str_transform as object check-in: 856cdd76cb user: olr tags: core, webext
Changes

Modified gc_core/js/text.js from [035788e8ab] to [c6c0eb070c].

     1      1   // JavaScript
     2      2   
     3      3   "use strict";
     4      4   
     5      5   const helpers = require("resource://grammalecte/helpers.js");
     6      6   
     7         -
     8         -function* getParagraph (sText) {
     9         -    // generator: returns paragraphs of text
    10         -    let iStart = 0;
    11         -    let iEnd = 0;
    12         -    sText = sText.replace("\r", "");
    13         -    while ((iEnd = sText.indexOf("\n", iStart)) !== -1) {
    14         -        yield sText.slice(iStart, iEnd);
    15         -        iStart = iEnd + 1;
    16         -    }
    17         -    yield sText.slice(iStart);
    18         -}
            7  +const text = {
            8  +    getParagraph: function* (sText) {
            9  +        // generator: returns paragraphs of text
           10  +        let iStart = 0;
           11  +        let iEnd = 0;
           12  +        sText = sText.replace("\r", "");
           13  +        while ((iEnd = sText.indexOf("\n", iStart)) !== -1) {
           14  +            yield sText.slice(iStart, iEnd);
           15  +            iStart = iEnd + 1;
           16  +        }
           17  +        yield sText.slice(iStart);
           18  +    },
    19     19   
    20         -function* wrap (sText, nWidth=80) {
    21         -    // generator: returns text line by line
    22         -    while (sText) {
    23         -        if (sText.length >= nWidth) {
    24         -            let nEnd = sText.lastIndexOf(" ", nWidth) + 1;
    25         -            if (nEnd > 0) {
    26         -                yield sText.slice(0, nEnd);
    27         -                sText = sText.slice(nEnd);
           20  +    wrap: function* (sText, nWidth=80) {
           21  +        // generator: returns text line by line
           22  +        while (sText) {
           23  +            if (sText.length >= nWidth) {
           24  +                let nEnd = sText.lastIndexOf(" ", nWidth) + 1;
           25  +                if (nEnd > 0) {
           26  +                    yield sText.slice(0, nEnd);
           27  +                    sText = sText.slice(nEnd);
           28  +                } else {
           29  +                    yield sText.slice(0, nWidth);
           30  +                    sText = sText.slice(nWidth);
           31  +                }
    28     32               } else {
    29         -                yield sText.slice(0, nWidth);
    30         -                sText = sText.slice(nWidth);
           33  +                break;
    31     34               }
    32         -        } else {
    33         -            break;
    34     35           }
    35         -    }
    36         -    yield sText;
    37         -}
           36  +        yield sText;
           37  +    },
           38  +
           39  +    getReadableError: function (oErr) {
           40  +        // Returns an error oErr as a readable error
           41  +        try {
           42  +            let s = "\n* " + oErr['nStart'] + ":" + oErr['nEnd'] + "  # " + oErr['sRuleId']+":\n";
           43  +            s += "  " + oErr["sMessage"];
           44  +            if (oErr["aSuggestions"].length > 0) {
           45  +                s += "\n  > Suggestions : " + oErr["aSuggestions"].join(" | ");
           46  +            }
           47  +            if (oErr["URL"] !== "") {
           48  +                s += "\n  > URL: " + oErr["URL"];
           49  +            }
           50  +            return s;
           51  +        }
           52  +        catch (e) {
           53  +            helpers.logerror(e);
           54  +            return "\n# Error. Data: " + oErr.toString();
           55  +        }
           56  +    },
    38     57   
    39         -function getReadableError (oErr) {
    40         -    // Returns an error oErr as a readable error
    41         -    try {
    42         -        let s = "\n* " + oErr['nStart'] + ":" + oErr['nEnd'] + "  # " + oErr['sRuleId']+":\n";
    43         -        s += "  " + oErr["sMessage"];
    44         -        if (oErr["aSuggestions"].length > 0) {
    45         -            s += "\n  > Suggestions : " + oErr["aSuggestions"].join(" | ");
           58  +    addHtmlEntities: function (sParagraph) {
           59  +        if (sParagraph.includes("&")) {
           60  +            sParagraph = sParagraph.replace(/&/g, "&");
    46     61           }
    47         -        if (oErr["URL"] !== "") {
    48         -            s += "\n  > URL: " + oErr["URL"];
           62  +        if (sParagraph.includes("<")) {
           63  +            sParagraph = sParagraph.replace(/</g, "&lt;").replace(/>/g, "&gt;");
    49     64           }
    50         -        return s;
    51         -    }
    52         -    catch (e) {
    53         -        helpers.logerror(e);
    54         -        return "\n# Error. Data: " + oErr.toString();
    55         -    }
    56         -}
           65  +        return sParagraph;
           66  +    },
           67  +
           68  +    createHTMLBlock: function (sParagraph, iParagraph) {
           69  +        let sClassName = (sParagraph.includes('<u id="err')) ? " softred" : "";
           70  +        return '<p id="paragr'+iParagraph.toString()+'" class="paragraph' + sClassName + '" lang="fr" spellcheck="false">' + sParagraph + '</p>\n'
           71  +               + '<div class="actions"><div id="end'+iParagraph.toString()+'" class="button red">×</div>'
           72  +               + '<div id="edit'+iParagraph.toString()+'" class="button">Éditer</div>'
           73  +               + '<div id="check'+iParagraph.toString()+'" class="button green">Réanalyser</div>&nbsp;</div>\n';
           74  +    },
    57     75   
    58         -function addHtmlEntities (sParagraph) {
    59         -    if (sParagraph.includes("&")) {
    60         -        sParagraph = sParagraph.replace(/&/g, "&amp;");
    61         -    }
    62         -    if (sParagraph.includes("<")) {
    63         -        sParagraph = sParagraph.replace(/</g, "&lt;").replace(/>/g, "&gt;");
    64         -    }
    65         -    return sParagraph;
    66         -}
           76  +    tagParagraph: function (sParagraph, iParagraph, aGrammErr, aSpellErr, bDebug=false) {
           77  +        // Returns a text with with underlined errors and messages in tooltip
           78  +        try {
           79  +            if (aGrammErr.length === 0  &&  aSpellErr.length === 0) {
           80  +                return sParagraph;
           81  +            }
           82  +            aGrammErr.push(...aSpellErr);
           83  +            aGrammErr.sort(function (a, b) {
           84  +                if (a["nStart"] < b["nStart"])
           85  +                    return 1;
           86  +                if (a["nStart"] > b["nStart"])
           87  +                    return -1;
           88  +                return 0;
           89  +            });
    67     90   
    68         -function createHTMLBlock (sParagraph, iParagraph) {
    69         -    let sClassName = (sParagraph.includes('<u id="err')) ? " softred" : "";
    70         -    return '<p id="paragr'+iParagraph.toString()+'" class="paragraph' + sClassName + '" lang="fr" spellcheck="false">' + sParagraph + '</p>\n'
    71         -           + '<div class="actions"><div id="end'+iParagraph.toString()+'" class="button red">×</div>'
    72         -           + '<div id="edit'+iParagraph.toString()+'" class="button">Éditer</div>'
    73         -           + '<div id="check'+iParagraph.toString()+'" class="button green">Réanalyser</div>&nbsp;</div>\n';
    74         -}
    75         -
    76         -function tagParagraph (sParagraph, iParagraph, aGrammErr, aSpellErr, bDebug=false) {
    77         -    // Returns a text with with underlined errors and messages in tooltip
    78         -    try {
    79         -        if (aGrammErr.length === 0  &&  aSpellErr.length === 0) {
           91  +            let nErr = aGrammErr.length - 1; // we count errors to give them an identifier
           92  +            let nStartLastErr = sParagraph.length;
           93  +            for (let oErr of aGrammErr) {
           94  +                let sErrId = iParagraph.toString() + "_" + nErr.toString();
           95  +                let nStart = oErr["nStart"];
           96  +                let nEnd = oErr["nEnd"];
           97  +                if (nEnd <= nStartLastErr) {
           98  +                    oErr["sId"] = sErrId;
           99  +                    if (oErr['sType'] !== 'WORD') {
          100  +                        // Grammar Error
          101  +                        sParagraph = sParagraph.slice(0, nStart)
          102  +                                   + '<u id="err' + sErrId + '" class="error '+oErr['sType']+'" href="#" onclick="return false;">'
          103  +                                   + sParagraph.slice(nStart, nEnd)
          104  +                                   + '<span id="tooltip' + sErrId + '" class="tooltip" contenteditable="false">'
          105  +                                   + this._getGrammarErrorHTML(oErr, bDebug) + '</span>'
          106  +                                   + "</u><!-- err_end -->" + sParagraph.slice(nEnd);
          107  +                    } else {
          108  +                        // Spelling error
          109  +                        sParagraph = sParagraph.slice(0, nStart)
          110  +                                   + '<u id="err' + sErrId + '" class="error spell" href="#" onclick="return false;">'
          111  +                                   + sParagraph.slice(nStart, nEnd)
          112  +                                   + '<span id="tooltip' + sErrId + '" class="tooltip" contenteditable="false">'
          113  +                                   + this._getSpellingErrorHTML(oErr) + '</span>'
          114  +                                   + "</u><!-- err_end -->" + sParagraph.slice(nEnd);
          115  +                    }
          116  +                    nStartLastErr = nStart;
          117  +                }
          118  +                nErr -= 1;
          119  +            }
    80    120               return sParagraph;
    81    121           }
    82         -        aGrammErr.push(...aSpellErr);
    83         -        aGrammErr.sort(function (a, b) {
    84         -            if (a["nStart"] < b["nStart"])
    85         -                return 1;
    86         -            if (a["nStart"] > b["nStart"])
    87         -                return -1;
    88         -            return 0;
    89         -        });
          122  +        catch (e) {
          123  +            helpers.logerror(e);
          124  +            return "# Error.";
          125  +        }
          126  +    },
    90    127   
    91         -        let nErr = aGrammErr.length - 1; // we count errors to give them an identifier
    92         -        let nStartLastErr = sParagraph.length;
    93         -        for (let oErr of aGrammErr) {
    94         -            let sErrId = iParagraph.toString() + "_" + nErr.toString();
    95         -            let nStart = oErr["nStart"];
    96         -            let nEnd = oErr["nEnd"];
    97         -            if (nEnd <= nStartLastErr) {
    98         -                oErr["sId"] = sErrId;
    99         -                if (oErr['sType'] !== 'WORD') {
   100         -                    // Grammar Error
   101         -                    sParagraph = sParagraph.slice(0, nStart)
   102         -                               + '<u id="err' + sErrId + '" class="error '+oErr['sType']+'" href="#" onclick="return false;">'
   103         -                               + sParagraph.slice(nStart, nEnd)
   104         -                               + '<span id="tooltip' + sErrId + '" class="tooltip" contenteditable="false">'
   105         -                               + getGrammarErrorHTML(oErr, bDebug) + '</span>'
   106         -                               + "</u><!-- err_end -->" + sParagraph.slice(nEnd);
   107         -                } else {
   108         -                    // Spelling error
   109         -                    sParagraph = sParagraph.slice(0, nStart)
   110         -                               + '<u id="err' + sErrId + '" class="error spell" href="#" onclick="return false;">'
   111         -                               + sParagraph.slice(nStart, nEnd)
   112         -                               + '<span id="tooltip' + sErrId + '" class="tooltip" contenteditable="false">'
   113         -                               + getSpellingErrorHTML(oErr) + '</span>'
   114         -                               + "</u><!-- err_end -->" + sParagraph.slice(nEnd);
          128  +    _getGrammarErrorHTML: function (oErr, bDebug=false) {
          129  +        // Returns an error oErr as a readable error in HTML
          130  +        try {
          131  +            let sErr = '';
          132  +            if (bDebug) {
          133  +                sErr += '<em class="debug">'
          134  +                  + '<i id="data'+oErr['sId']+'" class="data" hidden>'
          135  +                  + oErr['nStart'] + ":" + oErr['nEnd'] + ' · #' + oErr['sRuleId'] + " </i>+</em>";
          136  +            }
          137  +            sErr += oErr["sMessage"];
          138  +            sErr += ' <a id="ignore'+oErr['sId']+'" class="ignore" href="#" onclick="return false;">IGNORER</a>';
          139  +            if (oErr["URL"] !== "") {
          140  +                sErr += ' <a href="' + oErr["URL"] + '" onclick="return false;">Infos…</a>';
          141  +            }
          142  +            if (oErr["aSuggestions"].length > 0) {
          143  +                sErr += '<br/><s>Suggestions :</s><br/>';
          144  +                let iSugg = 0;
          145  +                for (let sSugg of oErr["aSuggestions"]) {
          146  +                    sErr += '<a id="sugg' + oErr['sId'] + "-" + iSugg + '" class="sugg" href="#" onclick="return false;">' + sSugg + '</a> ';
          147  +                    iSugg += 1;
   115    148                   }
   116         -                nStartLastErr = nStart;
   117    149               }
   118         -            nErr -= 1;
          150  +            return sErr;
          151  +        }
          152  +        catch (e) {
          153  +            helpers.logerror(e);
          154  +            return '# Error. Data: ' + oErr.toString();
          155  +        }
          156  +    },
          157  +
          158  +    _getSpellingErrorHTML: function (oErr) {
          159  +        // Returns an error oErr as a readable error in HTML
          160  +        try {
          161  +            let sErr = 'Mot inconnu du dictionnaire.';
          162  +            sErr += ' <a id="ignore' + oErr['sId'] + '" class="ignore" href="#" onclick="return false;">IGNORER</a>';
          163  +            sErr += '<br/><s>Suggestions :</s><br/>';
          164  +            return sErr;
   119    165           }
   120         -        return sParagraph;
   121         -    }
   122         -    catch (e) {
   123         -        helpers.logerror(e);
   124         -        return "# Error.";
   125         -    }
   126         -}
   127         -
   128         -function getGrammarErrorHTML (oErr, bDebug=false) {
   129         -    // Returns an error oErr as a readable error in HTML
   130         -    try {
   131         -        let sErr = '';
   132         -        if (bDebug) {
   133         -            sErr += '<em class="debug">'
   134         -              + '<i id="data'+oErr['sId']+'" class="data" hidden>'
   135         -              + oErr['nStart'] + ":" + oErr['nEnd'] + ' · #' + oErr['sRuleId'] + " </i>+</em>";
          166  +        catch (e) {
          167  +            helpers.logerror(e);
          168  +            return '# Error. Data: ' + oErr.toString();
   136    169           }
   137         -        sErr += oErr["sMessage"];
   138         -        sErr += ' <a id="ignore'+oErr['sId']+'" class="ignore" href="#" onclick="return false;">IGNORER</a>';
   139         -        if (oErr["URL"] !== "") {
   140         -            sErr += ' <a href="' + oErr["URL"] + '" onclick="return false;">Infos…</a>';
   141         -        }
   142         -        if (oErr["aSuggestions"].length > 0) {
   143         -            sErr += '<br/><s>Suggestions :</s><br/>';
   144         -            let iSugg = 0;
   145         -            for (let sSugg of oErr["aSuggestions"]) {
   146         -                sErr += '<a id="sugg' + oErr['sId'] + "-" + iSugg + '" class="sugg" href="#" onclick="return false;">' + sSugg + '</a> ';
   147         -                iSugg += 1;
   148         -            }
   149         -        }
   150         -        return sErr;
   151         -    }
   152         -    catch (e) {
   153         -        helpers.logerror(e);
   154         -        return '# Error. Data: ' + oErr.toString();
   155         -    }
   156         -}
   157         -
   158         -function getSpellingErrorHTML (oErr) {
   159         -    // Returns an error oErr as a readable error in HTML
   160         -    try {
   161         -        let sErr = 'Mot inconnu du dictionnaire.';
   162         -        sErr += ' <a id="ignore' + oErr['sId'] + '" class="ignore" href="#" onclick="return false;">IGNORER</a>';
   163         -        sErr += '<br/><s>Suggestions :</s><br/>';
   164         -        return sErr;
   165         -    }
   166         -    catch (e) {
   167         -        helpers.logerror(e);
   168         -        return '# Error. Data: ' + oErr.toString();
   169    170       }
   170    171   }
   171    172   
   172    173   
   173    174   if (typeof(exports) !== 'undefined') {
   174         -    exports.getParagraph = getParagraph;
   175         -    exports.addHtmlEntities = addHtmlEntities;
   176         -    exports.createHTMLBlock = createHTMLBlock;
   177         -    exports.tagParagraph = tagParagraph;
   178         -    exports.getReadableError = getReadableError;
          175  +    exports.getParagraph = text.getParagraph;
          176  +    exports.addHtmlEntities = text.addHtmlEntities;
          177  +    exports.createHTMLBlock = text.createHTMLBlock;
          178  +    exports.tagParagraph = text.tagParagraph;
          179  +    exports.getReadableError = text.getReadableError;
   179    180   }