book-source 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/StructuredDocument.js +31 -0
- package/lib/book-source.js +2 -1
- package/lib/textPostFilters.js +100 -0
- package/package.json +1 -1
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
const documentParts = require( './documentParts.js' ) ;
|
|
32
32
|
const Style = require( './Style.js' ) ;
|
|
33
33
|
const Theme = require( './Theme.js' ) ;
|
|
34
|
+
const textPostFilters = require( './textPostFilters.js' ) ;
|
|
34
35
|
|
|
35
36
|
const inPlaceFilter = require( 'array-kit/lib/inPlaceFilter.js' ) ;
|
|
36
37
|
|
|
@@ -217,6 +218,36 @@ StructuredDocument.prototype.getText = function( parts = this.parts ) {
|
|
|
217
218
|
|
|
218
219
|
|
|
219
220
|
|
|
221
|
+
StructuredDocument.prototype.textPostFilter = function( postFilterList , parts = this.parts ) {
|
|
222
|
+
for ( let postFilter of postFilterList ) {
|
|
223
|
+
if ( typeof postFilter === 'string' ) {
|
|
224
|
+
postFilter = textPostFilters[ postFilter ] ;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if ( typeof postFilter === 'function' ) {
|
|
228
|
+
this.oneTextPostFilter( postFilter , parts ) ;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
} ;
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
StructuredDocument.prototype.oneTextPostFilter = function( postFilter , parts , runtime = { lastPart: null } ) {
|
|
236
|
+
for ( let part of parts ) {
|
|
237
|
+
if ( ( part instanceof documentParts.Text ) && part.text ) {
|
|
238
|
+
postFilter( part , runtime.lastPart ) ;
|
|
239
|
+
|
|
240
|
+
// Can be modified by the callback
|
|
241
|
+
if ( part.text ) { runtime.lastPart = part ; }
|
|
242
|
+
}
|
|
243
|
+
else if ( part.parts?.length ) {
|
|
244
|
+
this.oneTextPostFilter( postFilter , part.parts , runtime ) ;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
} ;
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
220
251
|
// Parser
|
|
221
252
|
|
|
222
253
|
StructuredDocument.parse = function( str , options ) {
|
package/lib/book-source.js
CHANGED
|
@@ -34,10 +34,11 @@ module.exports = bookSource ;
|
|
|
34
34
|
bookSource.StructuredDocument = require( './StructuredDocument.js' ) ;
|
|
35
35
|
bookSource.Style = require( './Style.js' ) ;
|
|
36
36
|
bookSource.Theme = require( './Theme.js' ) ;
|
|
37
|
+
bookSource.textPostFilters = require( './textPostFilters.js' ) ;
|
|
37
38
|
|
|
38
39
|
Object.assign( bookSource , require( './documentParts.js' ) ) ;
|
|
39
40
|
|
|
40
|
-
// Exposed external
|
|
41
|
+
// Exposed external libs
|
|
41
42
|
const paletteShade = require( 'palette-shade' ) ;
|
|
42
43
|
bookSource.Color = paletteShade.Color ;
|
|
43
44
|
bookSource.Palette = paletteShade.Palette ;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Book Source
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2023 Cédric Ronvel
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
|
16
|
+
copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
"use strict" ;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// Non-breaking space
|
|
32
|
+
const NBSP = String.fromCharCode( 0xa0 ) ;
|
|
33
|
+
//const NBSP = '#' ;
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
const DOUBLE_GRAPH_PUNCTUATIONS = [ ':' , ';' , '!' , '?' ] ;
|
|
38
|
+
const SPACES = [ ' ' , NBSP ] ;
|
|
39
|
+
const DOUBLE_GRAPH_PUNCTUATIONS_AND_SPACES = DOUBLE_GRAPH_PUNCTUATIONS.concat( SPACES ) ;
|
|
40
|
+
const FRENCH_TYPO_PUNCTUATION_REGEX =
|
|
41
|
+
"([^" + DOUBLE_GRAPH_PUNCTUATIONS_AND_SPACES.join( '' ) + "])"
|
|
42
|
+
+ "[" + SPACES.join( '' ) + "]?"
|
|
43
|
+
+ "([" + DOUBLE_GRAPH_PUNCTUATIONS.join( '' ) + "]+)" ;
|
|
44
|
+
const FRENCH_TYPO_PUNCTUATION_REGEX_WITHOUT_PREVIOUS =
|
|
45
|
+
"[" + SPACES.join( '' ) + "]?"
|
|
46
|
+
+ "([" + DOUBLE_GRAPH_PUNCTUATIONS.join( '' ) + "]+)" ;
|
|
47
|
+
const FRENCH_TYPO_PUNCTUATION_REGEX_ALT_START =
|
|
48
|
+
"(?:"
|
|
49
|
+
+ "^" + FRENCH_TYPO_PUNCTUATION_REGEX_WITHOUT_PREVIOUS
|
|
50
|
+
+ "|(?<!^)" + FRENCH_TYPO_PUNCTUATION_REGEX
|
|
51
|
+
+ ")" ;
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
exports['french-typo'] = exports.frenchTypo = ( part , lastPart ) => {
|
|
56
|
+
var useRegexAltStart = false ;
|
|
57
|
+
|
|
58
|
+
if ( lastPart ) {
|
|
59
|
+
let firstChar = part.text[ 0 ] ;
|
|
60
|
+
let lastChar = lastPart.text[ lastPart.text.length - 1 ] ;
|
|
61
|
+
|
|
62
|
+
if (
|
|
63
|
+
DOUBLE_GRAPH_PUNCTUATIONS.includes( firstChar )
|
|
64
|
+
&& ! DOUBLE_GRAPH_PUNCTUATIONS.includes( lastChar )
|
|
65
|
+
&& lastChar !== NBSP
|
|
66
|
+
) {
|
|
67
|
+
if ( lastChar === ' ' ) {
|
|
68
|
+
lastPart.text = lastPart.text.slice( 0 , - 1 ) + NBSP ;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
part.text = NBSP + part.text ;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if ( ! DOUBLE_GRAPH_PUNCTUATIONS_AND_SPACES.includes( lastChar ) ) {
|
|
75
|
+
useRegexAltStart = true ;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if ( useRegexAltStart ) {
|
|
80
|
+
part.text = part.text.replace(
|
|
81
|
+
new RegExp( FRENCH_TYPO_PUNCTUATION_REGEX_ALT_START , 'g' ) ,
|
|
82
|
+
( match , punctuations1 , previous , punctuations2 ) =>
|
|
83
|
+
punctuations1 ? NBSP + punctuations1 :
|
|
84
|
+
previous + NBSP + punctuations2
|
|
85
|
+
) ;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
part.text = part.text.replace(
|
|
89
|
+
new RegExp( FRENCH_TYPO_PUNCTUATION_REGEX , 'g' ) ,
|
|
90
|
+
( match , previous , punctuations ) => previous + NBSP + punctuations
|
|
91
|
+
) ;
|
|
92
|
+
}
|
|
93
|
+
} ;
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
exports.apostrophe = ( part ) => {
|
|
98
|
+
part.text = part.text.replace( /'/g , '’' ) ;
|
|
99
|
+
} ;
|
|
100
|
+
|