@@ -4,9 +4,10 @@ module.exports = function( grunt ) {
44 var _ = require ( "underscore" ) ,
55 cheerio = require ( "cheerio" ) ,
66
7- replaceCombinedCssReference = function ( content , processedName ) {
8- return content . replace ( / \. \. \/ c s s \/ / , "css/" )
9- . replace ( / j q u e r y \. m o b i l e \. c s s / gi, processedName + ".min.css" ) ;
7+ replaceCombinedCssReference = function ( href , processedName ) {
8+ return href
9+ . replace ( / \. \. \/ c s s / , "css" )
10+ . replace ( / j q u e r y \. m o b i l e \. c s s / , processedName + ".min.css" ) ;
1011 } ,
1112
1213 // Ensure that modules specified via the --modules option are in the same
@@ -391,37 +392,96 @@ module.exports = function( grunt ) {
391392 "demos.processed" : {
392393 options : {
393394 processContent : function ( content , srcPath ) {
394- var processedName = grunt . config . process ( name + "<%= versionSuffix %>" ) ;
395- content = content . replace ( / _ a s s e t s \/ j s \/ " > / gi, "_assets/js/index.js\">" ) ;
396- content = content . replace ( / \. \. \/ e x t e r n a l \/ j q u e r y \/ / gi, "js/" ) ;
397- content = content . replace ( / \. \. \/ j s \/ \" / gi, "js/\"" ) ;
398- content = content . replace ( / j s \/ " / gi, "js/" + processedName + ".min.js\"" ) ;
399- content = replaceCombinedCssReference ( content , processedName ) ;
395+ var processedName , $ ;
396+
400397 content = content . replace ( / ^ \s * < \? p h p i n c l u d e \( \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] .* $ / gmi,
401398 function ( match , includePath /*, offset, string */ ) {
402- var fileToInclude , newSrcPath = srcPath ;
399+ var newSrcPath = srcPath ;
403400
404401 // If we've already handled the nested includes use the version
405402 // that was copied to the dist folder
406403 // TODO use the config from copy:demos.nested.files
407- if ( includePath . match ( / j q m \- c o n t e n t s . p h p | j q m \- n a v m e n u . p h p | j q m \- s e a r c h .p h p / ) ) {
404+ if ( includePath . match ( / j q m \- ( c o n t e n t s | n a v m e n u | s e a r c h ) \ .p h p / ) ) {
408405 newSrcPath = "dist/" + newSrcPath ;
409406 }
410407
411- fileToInclude = path . resolve ( path . join ( path . dirname ( newSrcPath ) , includePath ) ) ;
412-
413- return grunt . file . read ( fileToInclude ) ;
408+ return grunt . file . read ( path . resolve ( path . join (
409+ path . dirname ( newSrcPath ) , includePath ) ) ) ;
414410 }
415411 ) ;
416- content = content . replace ( / \. p h p / gi, ".html" ) ;
417412
418- // Demos that separately refer to the structure need to be processed here
419- content = content . replace ( / c s s \/ s t r u c t u r e \/ j q u e r y \. m o b i l e \. s t r u c t u r e \. c s s / gi,
420- path . join ( "css" , "themes" , "default" , processedName + ".structure" + ".min.css" ) ) ;
413+ if ( content . substring ( 0 , 15 ) . toLowerCase ( ) === "<!doctype html>" || srcPath . match ( / \. p h p $ / ) ) {
414+ processedName = grunt . config . process ( name + "<%= versionSuffix %>" ) ;
415+ $ = cheerio . load ( content ) ;
416+ $ ( "script" ) . each ( function ( ) {
417+ var text ,
418+ element = $ ( this ) ,
419+ src = element . attr ( "src" ) ;
420+
421+ if ( src ) {
422+ element . attr ( "src" , src
423+ . replace ( / _ a s s e t s \/ j s \/ ? $ / , "_assets/js/index.js" )
424+ . replace ( / \. \. \/ e x t e r n a l \/ j q u e r y \/ j q u e r y .j s $ / ,
425+ "js/jquery.js" )
426+ . replace ( / \. \. \/ j s \/ ? $ / ,
427+ "js/" + processedName + ".min.js" )
428+ . replace ( / ^ j s \/ ? $ / , "demos/js/" + processedName + ".min.js" ) ) ;
429+ } else {
430+ text = element . text ( ) ;
431+
432+ // References to stylesheets via grunticon need to be updated
433+ text = text . replace ( / ( g r u n t i c o n \( \[ ( [ ^ \] ] * ) ) / ,
434+ function ( match , group ) {
435+ var index ,
436+ offset = group . indexOf ( "[" ) ,
437+ prefix = group . substring ( 0 , offset + 1 ) ;
438+
439+ group = group . substring ( offset + 1 ) . split ( "," ) ;
440+
441+ for ( index in group ) {
442+ group [ index ] = "\"" + group [ index ]
443+ . trim ( )
444+ . replace ( / ( ^ [ ' " ] ) | ( [ ' " ] $ ) / g, "" )
445+ . replace ( / \. \. \/ c s s \/ / , "css/" )
446+ . replace ( / \. c s s $ / , ".min.css" ) + "\"" ;
447+ }
448+
449+ return prefix + " " + group . join ( "," ) + " " ;
450+ } ) ;
451+
452+ //element.html( text );
453+ element [ 0 ] . children [ 0 ] . data = text ;
454+ }
455+ } ) ;
456+
457+ $ ( "link[rel='stylesheet'][href]" ) . each ( function ( ) {
458+ var element = $ ( this ) ;
459+
460+ element . attr ( "href" ,
461+ replaceCombinedCssReference ( element . attr ( "href" ) ,
462+ processedName )
463+
464+ // Demos that separately refer to the structure need to be
465+ // processed here
466+ . replace ( / c s s \/ s t r u c t u r e \/ j q u e r y \. m o b i l e \. s t r u c t u r e \. c s s / gi,
467+ path . join ( "css" , "themes" , "default" ,
468+ processedName + ".structure" + ".min.css" ) )
469+
470+ // References to the icons CSS file need to be processed here
471+ . replace ( / c s s \/ t h e m e s \/ d e f a u l t \/ j q u e r y \. m o b i l e \. i c o n s \. c s s / ,
472+ path . join ( ".." , "jquery.mobile.icons.min.css" ) ) ) ;
421473
422- // References to the icons CSS file need to be processed here
423- content = content . replace ( / c s s \/ t h e m e s \/ d e f a u l t \/ j q u e r y \. m o b i l e \. i c o n s \. c s s / gi,
424- path . join ( ".." , "jquery.mobile.icons.min.css" ) ) ;
474+ } ) ;
475+
476+ $ ( "a[href]" ) . each ( function ( ) {
477+ var element = $ ( this ) ;
478+
479+ element . attr ( "href" ,
480+ element . attr ( "href" ) . replace ( / \. p h p $ / , ".html" ) ) ;
481+ } ) ;
482+
483+ content = $ . html ( ) ;
484+ }
425485 return content ;
426486 }
427487 } ,
@@ -442,10 +502,16 @@ module.exports = function( grunt ) {
442502
443503 if ( / \. h t m l $ / . test ( srcPath ) ) {
444504
445- content = replaceCombinedCssReference ( content , processedName ) ;
446-
447505 $ = cheerio . load ( content ) ;
448506
507+ $ ( "link[rel='stylesheet'][href]" ) . each ( function ( ) {
508+ var element = $ ( this ) ;
509+
510+ element . attr ( "href" ,
511+ replaceCombinedCssReference ( element . attr ( "href" ) ,
512+ processedName ) ) ;
513+ } ) ;
514+
449515 $ ( "script" ) . each ( function ( idx , element ) {
450516 var script = $ ( element ) ;
451517 if ( / r e q u i r e j s \. c o n f i g \. j s $ / . test ( script . attr ( "src" ) ) ) {
0 commit comments