@@ -2,106 +2,104 @@ const fs = require('fs');
22var dedent = require ( 'dedent' ) ;
33const prettier = require ( "prettier" ) ;
44const vscode = require ( 'vscode' ) ;
5- const { format } = require ( 'path' ) ;
6- const { privateEncrypt } = require ( 'crypto' ) ;
75
8- function fileParser ( file , writePath , fileName , _callback ) {
9- let arrayOfItemCode = [ ]
6+ function fileParser ( file , writePath , fileName ) {
7+ return new Promise ( ( resolve , reject ) => {
8+ let arrayOfItemCode = [ ] ;
109
11- let regStart = / (?< = @ < r ) \w + /
10+ let regStart = / (?< = @ < r ) \w + / ;
1211
13- fs . readFile ( file , ' utf-8' , ( err , data ) => {
14- if ( err ) throw err ;
12+ fs . readFile ( file , " utf-8" , ( err , data ) => {
13+ if ( err ) reject ( err ) ;
1514
16- let thisID ;
15+ let thisID ;
1716
18- const lines = data . split ( '\n' ) ;
19-
20- let language = fileName . split ( "." ) . pop ( )
17+ const lines = data . split ( "\n" ) ;
2118
22- lines . forEach ( ( line ) => {
23- if ( line . includes ( "@/w" ) ) {
24- let singleLineReg = / ( .* ) @ \/ w ( \S + ) \s * ( .* ) /
19+ let language = fileName . split ( "." ) . pop ( ) ;
2520
26- thisID = singleLineReg . exec ( line ) [ 2 ]
27- let singleLCode = singleLineReg . exec ( line ) [ 1 ] . trim ( )
28- let singleLExplication = singleLineReg . exec ( line ) [ 3 ]
21+ lines . forEach ( ( line ) => {
22+ if ( line . includes ( "@/w" ) ) {
23+ let singleLineReg = / ( . * ) @ \/ w ( \S + ) \s * ( . * ) / ;
2924
30- for ( let i = 0 ; i < arrayOfItemCode . length ; i ++ ) {
31- if ( arrayOfItemCode [ i ] . getId ( ) == thisID ) {
32- arrayOfItemCode [ i ] . explication = singleLExplication
25+ thisID = singleLineReg . exec ( line ) [ 2 ] ;
26+ let singleLCode = singleLineReg . exec ( line ) [ 1 ] . trim ( ) ;
27+ let singleLExplication = singleLineReg . exec ( line ) [ 3 ] ;
28+
29+ for ( let i = 0 ; i < arrayOfItemCode . length ; i ++ ) {
30+ if ( arrayOfItemCode [ i ] . getId ( ) == thisID ) {
31+ arrayOfItemCode [ i ] . explication = singleLExplication ;
32+ }
3333 }
34- }
3534
36- let item = new itemCode ( thisID )
35+ let item = new itemCode ( thisID ) ;
3736
38- item . code = removeComments ( singleLCode , language )
37+ item . code = removeComments ( singleLCode , language ) ;
3938
40- item . explication = singleLExplication
39+ item . explication = singleLExplication ;
4140
42- if ( item . code != "" ) {
43- arrayOfItemCode . push ( item )
41+ if ( item . code != "" ) {
42+ arrayOfItemCode . push ( item ) ;
43+ }
4444 }
45- }
4645
47- if ( line . includes ( "@<r" ) ) {
48- thisID = regStart . exec ( line ) [ 0 ]
49- let isPresent = false
46+ if ( line . includes ( "@<r" ) ) {
47+ thisID = regStart . exec ( line ) [ 0 ] ;
48+ let isPresent = false ;
5049
51- for ( let i = 0 ; i < arrayOfItemCode . length ; i ++ ) {
52- if ( arrayOfItemCode [ i ] . getId ( ) == thisID ) {
53- isPresent = true
50+ for ( let i = 0 ; i < arrayOfItemCode . length ; i ++ ) {
51+ if ( arrayOfItemCode [ i ] . getId ( ) == thisID ) {
52+ isPresent = true ;
53+ }
5454 }
55- }
5655
57- if ( ! isPresent ) {
58- let item = new itemCode ( thisID )
56+ if ( ! isPresent ) {
57+ let item = new itemCode ( thisID ) ;
5958
60- let customRegID = `@<r${ thisID } \\s+([^@\n]+)\n\\s*([\\s\\S]*?)\\s*@r>${ thisID } `
61- customRegID = new RegExp ( customRegID )
59+ let customRegID = `@<r${ thisID } \\s+([^@\n]+)\n\\s*([\\s\\S]*?)\\s*@r>${ thisID } ` ;
60+ customRegID = new RegExp ( customRegID ) ;
6261
62+ let extractedCode = "" ;
6363
64- let extractedCode = ""
65-
66- try {
67- extractedCode = customRegID . exec ( data ) [ 2 ]
6864 try {
69- extractedCode = removeComments ( extractedCode , language )
65+ extractedCode = customRegID . exec ( data ) [ 2 ] ;
66+ try {
67+ extractedCode = removeComments ( extractedCode , language ) ;
68+ } catch ( error ) {
69+ console . log ( error ) ;
70+ }
71+
72+ extractedCode = removeEmptyLines ( extractedCode ) ;
73+
74+ // TODO: support more langus
75+ extractedCode = dedent ( extractedCode ) ;
76+
77+ try {
78+ item . code = extractedCode ;
79+ } catch ( error ) {
80+ console . log ( error ) ;
81+ }
82+
83+ item . explication = customRegID . exec ( data ) [ 1 ] ;
84+
85+ arrayOfItemCode . push ( item ) ;
7086 } catch ( error ) {
71- console . log ( error )
87+ console . log ( "Error here" ) ;
7288 }
73-
74- extractedCode = removeEmptyLines ( extractedCode )
75-
76- // TODO: support more langus
77- extractedCode = dedent ( extractedCode )
78- try {
79- item . code = extractedCode
80- } catch ( error ) {
81- console . log ( error )
82- }
83-
84- item . explication = customRegID . exec ( data ) [ 1 ]
85-
86- arrayOfItemCode . push ( item )
87- } catch ( error ) {
88- console . log ( "Error here" )
8989 }
90-
9190 }
92- }
93- } )
94-
95-
96- _callback ( arrayOfItemCode , writePath , fileName )
97- } )
91+ } ) ;
92+ resolve ( arrayOfItemCode ) ;
93+ } ) ;
94+ } ) ;
9895}
9996
97+
10098class itemCode {
10199 constructor ( id , code , explication ) {
102- this . id = id ;
103- this . code = code ;
104- this . explication = explication ;
100+ this . id = id ;
101+ this . code = code ;
102+ this . explication = explication ;
105103 }
106104
107105 getId ( ) {
@@ -111,63 +109,61 @@ class itemCode {
111109
112110
113111function checkLanguage ( language ) {
114- switch ( language ) {
115- case "py" :
116- return "#"
117- case "ru" :
118- return "#"
119- case "hs" :
120- return "- -"
112+ switch ( language ) {
113+ case "py" :
114+ return "#"
115+ case "ru" :
116+ return "#"
117+ case "hs" :
118+ return "- -"
121119 case "lhs" :
122120 return "- -"
123- case "r" :
124- return "#"
125- case "erl" :
126- return "%"
121+ case "r" :
122+ return "#"
123+ case "erl" :
124+ return "%"
127125 case "hrl" :
128126 return "%"
129- case "pl" :
130- return "#"
131- case "html" :
132- return null // waj3it ras for now
133- case "css" :
134- return null // waj3it ras for now
135- default :
136- return "//"
137- }
127+ case "pl" :
128+ return "#"
129+ case "html" :
130+ return null // waj3it ras for now
131+ case "css" :
132+ return null // waj3it ras for now
133+ default :
134+ return "//"
135+ }
138136}
139137
140138function removeComments ( code , language ) {
141- switch ( language ) {
142- case "py" :
143- return code . replace ( / # .* / g, '' )
144- case "ru" :
145- return code . replace ( / # .* / g, '' )
146- case "hs" :
147- return code . replace ( / - - .* / g, '' )
139+ switch ( language ) {
140+ case "py" :
141+ return code . replace ( / # .* / g, '' )
142+ case "ru" :
143+ return code . replace ( / # .* / g, '' )
144+ case "hs" :
145+ return code . replace ( / - - .* / g, '' )
148146 case "lhs" :
149147 return code . replace ( / - - .* / g, '' )
150- case "r" :
151- return code . replace ( / # .* / g, '' )
152- case "erl" :
153- return code . replace ( / % .* / g, '' )
148+ case "r" :
149+ return code . replace ( / # .* / g, '' )
150+ case "erl" :
151+ return code . replace ( / % .* / g, '' )
154152 case "hrl" :
155153 return code . replace ( / % .* / g, '' )
156- case "pl" :
157- return code . replace ( / # .* / g, '' )
158- case "html" :
159- return null // waj3it ras for now
160- case "css" :
161- return null // waj3it ras for now
162- default :
163- return code . replace ( / \/ \/ .* | \/ \* [ \s \S ] * ?\* \/ / g, '' )
164- }
154+ case "pl" :
155+ return code . replace ( / # .* / g, '' )
156+ case "html" :
157+ return null // waj3it ras for now
158+ case "css" :
159+ return null // waj3it ras for now
160+ default :
161+ return code . replace ( / \/ \/ .* | \/ \* [ \s \S ] * ?\* \/ / g, '' )
162+ }
165163}
166164
167165function removeEmptyLines ( str ) {
168166 return str . split ( / \r ? \n / ) . filter ( line => line . trim ( ) !== '' ) . join ( '\n' )
169167}
170-
171-
172168
173169module . exports = { fileParser } ;
0 commit comments