aboutsummaryrefslogtreecommitdiff
path: root/app.js
blob: 31cbdd1e7d872dd80cdd7b56268ba5488b0193b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';


const START = '[__';
const END   = '__]';

function locator(value, fromIndex) {
  var index = value.indexOf(START, fromIndex);
  return index;
}

function plugin() {
  function inlineTokenizer(eat, value, silent) {
    if (!this.options.gfm || !value.startsWith(START)) {
      return;
    }

    var character = '';
    var previous = '';
    var preceding = '';
    var subvalue = '';
    var index = 1;
    var length = value.length;
    var now = eat.now();
    now.column += 2;
    now.offset += 2;

    while (!value.startsWith(END, index) && ++index < length) {
      subvalue+=value.charAt(index);
      if( value.charAt(index) == '\n' )
        return true;

    }
    let indexNext = index+END.length;
    let prop = {key:{}, classes:[],id:""}
    let lets_eat = "";
    if( value.charAt(indexNext) == '{' ) {
      indexNext++;
      lets_eat="{"

      while(true) {

        var type = 0;
        while(' \t\n\r\v'.indexOf(value.charAt(indexNext)) >= 0) {
            lets_eat+=value.charAt(indexNext);
            indexNext++;
        }
        if( value.charAt(indexNext) == '}' ) {
          break;
        } else if( value.charAt(indexNext) == '.' ) { // Classes
          type = 1;
          indexNext++;
          lets_eat+='.'
        } else if( value.charAt(indexNext) == '#' ) { // ID
          type = 2;
          indexNext++;
          lets_eat+='#'
        } else { // Key
          type = 3;
        }
        let labelFirst = "";
        let labelSecond = "";

        // Extract name
        while( '=\t\b\r\v  }'.indexOf(value.charAt(indexNext)) < 0 ) {
          labelFirst+=value.charAt(indexNext);
          indexNext++;
        }

        lets_eat+=labelFirst;
        if( value.charAt(indexNext) == '=' ) { // Set labelSecond
          indexNext++;
          lets_eat+='=';

          if( value.charAt(indexNext) == '"' ) {
            indexNext++;
            lets_eat+='"';
            while('"}\n'.indexOf(value.charAt(indexNext)) < 0) {
              labelSecond+=value.charAt(indexNext);
              indexNext++;
            }
            lets_eat+=labelSecond;
            if( value.charAt(indexNext) != '"' ) {
              // Erreur
            }else{
              indexNext++;
              lets_eat+='"';
            }
          } else if( value.charAt(indexNext) == "'" ) {
            indexNext++;
            lets_eat+="'";
            while("'}\n".indexOf(value.charAt(indexNext)) < 0) {
              labelSecond+=value.charAt(indexNext);
              indexNext++;
            }
            lets_eat+=labelSecond;
            if( value.charAt(indexNext) !="'" ) {
              // Erreur
            }else{
              indexNext++;
              lets_eat+="'";
            }
          } else {
            while(' \t\n\r\v=}'.indexOf(value.charAt(indexNext)) < 0) {
              labelSecond+=value.charAt(indexNext);
              indexNext++;
            }
            lets_eat+=labelSecond;
          }
        }
        if( labelSecond ) 
          console.log("{{" + labelFirst + "=" + labelSecond + "}}");
        else
          console.log("{{" + labelFirst + "}}");
      }
      lets_eat+="}";
    }
        /* istanbul ignore if - never used (yet) */
      if (silent) return true;

    if(  index < length )
      return eat(START + subvalue.slice(1) + END.slice(1)+lets_eat)({
        type: 'line-input',
        children: [],
        data: {
          hName: 'input',
          hProperties: {
            type: 'text',
            placeholder: subvalue.replace(/^_*/g, '').replace(/_*$/g, '')
          }
        }
      });
    else 
      return true;

  }

  inlineTokenizer.locator = locator;

  var Parser = this.Parser;

  // Inject inlineTokenizer
  var inlineTokenizers = Parser.prototype.inlineTokenizers;
  var inlineMethods = Parser.prototype.inlineMethods;
  inlineTokenizers.input = inlineTokenizer;
  inlineMethods.splice(inlineMethods.indexOf('url'), 0, 'input');

  var Compiler = this.Compiler;

  // Stringify
  if (Compiler) {
    var visitors = Compiler.prototype.visitors;
    visitors.lineinput = function (node) {
      return '[__' + this.all(node).join('') + '__]';
    };
  }
}

module.exports = plugin;