1- import { Song } from ' ./Song' ;
2- import { Section } from ' ./Section' ;
3- import { Line } from ' ./Line' ;
4- import { chunk , flatten , trim } from ' lodash' ;
5- import { Part } from ' ./Part' ;
1+ import { Song } from " ./Song" ;
2+ import { Section } from " ./Section" ;
3+ import { Line } from " ./Line" ;
4+ import { chunk , flatten , trim } from " lodash" ;
5+ import { Part } from " ./Part" ;
66
77export class SongPro {
8- static parse ( text ) {
9- let song = new Song ( ) ;
10- let currentSection = null ;
11-
12- const lines = text . split ( '\n' ) ;
13-
14- for ( let i = 0 ; i < lines . length ; i ++ ) {
15- let text = lines [ i ] ;
16-
17- if ( text . startsWith ( '@' ) ) {
18- this . processAttribute ( song , text ) ;
19- } else if ( text . startsWith ( '!' ) ) {
20- this . processCustomAttribute ( song , text ) ;
21- } else if ( text . startsWith ( '#' ) ) {
22- currentSection = this . processSection ( song , text ) ;
23- } else {
24- this . processLyricsAndChords ( song , currentSection , text ) ;
25- }
26- }
27-
28- return song ;
8+ static parse ( text ) {
9+ const song = new Song ( ) ;
10+ let currentSection = null ;
11+
12+ const lines = text . split ( "\n" ) ;
13+
14+ for ( let i = 0 ; i < lines . length ; i ++ ) {
15+ const text = lines [ i ] ;
16+
17+ if ( text . startsWith ( "@" ) ) {
18+ this . processAttribute ( song , text ) ;
19+ } else if ( text . startsWith ( "!" ) ) {
20+ this . processCustomAttribute ( song , text ) ;
21+ } else if ( text . startsWith ( "#" ) ) {
22+ currentSection = this . processSection ( song , text ) ;
23+ } else {
24+ this . processLyricsAndChords ( song , currentSection , text ) ;
25+ }
2926 }
3027
31- static processAttribute ( song , line ) {
32- const matches = / @ ( \w * ) = ( [ ^ % ] * ) / . exec ( line ) ;
28+ return song ;
29+ }
3330
34- if ( matches == null ) {
35- return ;
36- }
31+ static processAttribute ( song , line ) {
32+ const matches = / @ ( \w * ) = ( [ ^ % ] * ) / . exec ( line ) ;
3733
38- song . attrs [ matches [ 1 ] ] = matches [ 2 ] ;
34+ if ( matches == null ) {
35+ return ;
3936 }
4037
41- static processCustomAttribute ( song , line ) {
42- const matches = / ! ( \w * ) = ( [ ^ % ] * ) / . exec ( line ) ;
38+ song . attrs [ matches [ 1 ] ] = matches [ 2 ] ;
39+ }
4340
44- if ( matches == null ) {
45- return ;
46- }
41+ static processCustomAttribute ( song , line ) {
42+ const matches = / ! ( \w * ) = ( [ ^ % ] * ) / . exec ( line ) ;
4743
48- song . custom [ matches [ 1 ] ] = matches [ 2 ] ;
44+ if ( matches == null ) {
45+ return ;
4946 }
5047
51- static processSection ( song , line ) {
52- const matches = / # \s * ( [ ^ $ ] * ) / . exec ( line ) ;
48+ song . custom [ matches [ 1 ] ] = matches [ 2 ] ;
49+ }
5350
54- if ( matches == null ) {
55- return ;
56- }
51+ static processSection ( song , line ) {
52+ const matches = / # \s * ( [ ^ $ ] * ) / . exec ( line ) ;
5753
58- let name = matches [ 1 ] ;
59- let currentSection = new Section ( name ) ;
60- song . sections . push ( currentSection ) ;
61-
62- return currentSection ;
54+ if ( matches == null ) {
55+ return ;
6356 }
6457
65- static processLyricsAndChords ( song , currentSection , text ) {
66- if ( text === '' ) {
67- return ;
68- }
58+ const name = matches [ 1 ] ;
59+ const currentSection = new Section ( name ) ;
60+ song . sections . push ( currentSection ) ;
6961
70- if ( currentSection == null ) {
71- currentSection = new Section ( '' ) ;
72- song . sections . push ( currentSection ) ;
73- }
62+ return currentSection ;
63+ }
7464
75- let line = new Line ( ) ;
65+ static processLyricsAndChords ( song , currentSection , text ) {
66+ if ( text === "" ) {
67+ return ;
68+ }
7669
77- let captures = this . scan ( text , / ( \[ [ \w # b / ] + ] ) ? ( [ \w \s ' , . ! ( ) _ \- " ] * ) / gi) ;
78- captures = flatten ( captures ) ;
79- let groups = chunk ( captures , 2 ) ;
70+ if ( currentSection == null ) {
71+ currentSection = new Section ( "" ) ;
72+ song . sections . push ( currentSection ) ;
73+ }
8074
75+ const line = new Line ( ) ;
8176
82- for ( let i = 0 ; i < groups . length ; i ++ ) {
83- let group = groups [ i ] ;
84- let chord = group [ 0 ] ;
85- let lyric = group [ 1 ] ;
77+ let captures = this . scan ( text , / ( \[ [ \w # b / ] + ] ) ? ( [ \w \s ' , . ! ( ) _ \- " ] * ) / gi) ;
78+ captures = flatten ( captures ) ;
79+ const groups = chunk ( captures , 2 ) ;
8680
87- let part = new Part ( ) ;
81+ for ( let i = 0 ; i < groups . length ; i ++ ) {
82+ const group = groups [ i ] ;
83+ let chord = group [ 0 ] ;
84+ let lyric = group [ 1 ] ;
8885
89- if ( chord ) {
90- chord = chord . replace ( '[' , '' ) . replace ( ']' , '' ) ;
91- }
86+ const part = new Part ( ) ;
9287
93- if ( chord === undefined ) {
94- chord = '' ;
95- }
96- if ( lyric === undefined ) {
97- lyric = '' ;
98- }
88+ if ( chord ) {
89+ chord = chord . replace ( "[" , "" ) . replace ( "]" , "" ) ;
90+ }
9991
100- part . chord = trim ( chord ) ;
101- part . lyric = trim ( lyric ) ;
92+ if ( chord === undefined ) {
93+ chord = "" ;
94+ }
95+ if ( lyric === undefined ) {
96+ lyric = "" ;
97+ }
10298
103- if ( ! ( part . chord === '' && part . lyric === '' ) ) {
104- line . parts . push ( part ) ;
105- }
106- }
99+ part . chord = trim ( chord ) ;
100+ part . lyric = trim ( lyric ) ;
107101
108- currentSection . lines . push ( line ) ;
102+ if ( ! ( part . chord === "" && part . lyric === "" ) ) {
103+ line . parts . push ( part ) ;
104+ }
109105 }
110106
111- static scan ( string , regex ) {
112- if ( ! regex . global ) throw new Error ( 'regex must have \'global\' flag set' ) ;
113- var results = [ ] ;
114- string . replace ( regex , function ( ) {
115- results . push ( Array . prototype . slice . call ( arguments , 1 , - 2 ) ) ;
116- } ) ;
117- return results ;
118- }
119- }
107+ currentSection . lines . push ( line ) ;
108+ }
109+
110+ static scan ( string , regex ) {
111+ if ( ! regex . global ) throw new Error ( "regex must have 'global' flag set" ) ;
112+ const results = [ ] ;
113+ string . replace ( regex , function ( ) {
114+ results . push ( Array . prototype . slice . call ( arguments , 1 , - 2 ) ) ;
115+ } ) ;
116+ return results ;
117+ }
118+ }
0 commit comments