book-source 0.3.3 → 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.
@@ -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 ) {
@@ -1100,6 +1131,7 @@ function parseTableRowSeparator( str , ctx , thick = false ) {
1100
1131
 
1101
1132
  if ( str[ currentBar + 1 ] === '-' && str[ currentBar + 2 ] === ' ' && str[ nextBar - 1 ] === '-' && str[ nextBar - 2 ] === ' ' ) {
1102
1133
  // This is a rowSpan
1134
+ table.hasRowSpan = true ;
1103
1135
  ctx.rowSpanTables.add( table ) ;
1104
1136
  if ( ! tableRow.continueRowSpan ) { tableRow.continueRowSpan = [] ; }
1105
1137
 
@@ -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 lib
41
+ // Exposed external libs
41
42
  const paletteShade = require( 'palette-shade' ) ;
42
43
  bookSource.Color = paletteShade.Color ;
43
44
  bookSource.Palette = paletteShade.Palette ;
@@ -345,6 +345,7 @@ function Table() {
345
345
  this.multilineRowMode = false ;
346
346
  this.hasHeadSeparator = false ;
347
347
  this.hasRowSeparator = false ;
348
+ this.hasRowSpan = false ; // Useful to disable background colors based on odd/even rows
348
349
  BlockPart.call( this ) ;
349
350
  }
350
351
 
@@ -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
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "book-source",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "A lightweight markup language, inspired by Markdown.",
5
5
  "main": "lib/book-source.js",
6
6
  "directories": {