178a6aeb1SAndreas Gohr<?php 278a6aeb1SAndreas Gohr/** 378a6aeb1SAndreas Gohr * DokuWiki StyleSheet creator 478a6aeb1SAndreas Gohr * 578a6aeb1SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 678a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 778a6aeb1SAndreas Gohr */ 878a6aeb1SAndreas Gohr 9d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../'); 101c2d1019SAndreas Gohrif(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching) 1198bda4fdSAndreas Gohrif(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here 126c47a78cSAnika Henkeif(!defined('NL')) define('NL',"\n"); 1378a6aeb1SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php'); 1478a6aeb1SAndreas Gohr 1578a6aeb1SAndreas Gohr// Main (don't run when UNIT test) 1678a6aeb1SAndreas Gohrif(!defined('SIMPLE_TEST')){ 1778a6aeb1SAndreas Gohr header('Content-Type: text/css; charset=utf-8'); 1878a6aeb1SAndreas Gohr css_out(); 1978a6aeb1SAndreas Gohr} 2078a6aeb1SAndreas Gohr 2178a6aeb1SAndreas Gohr 2278a6aeb1SAndreas Gohr// ---------------------- functions ------------------------------ 2378a6aeb1SAndreas Gohr 2478a6aeb1SAndreas Gohr/** 2578a6aeb1SAndreas Gohr * Output all needed Styles 2678a6aeb1SAndreas Gohr * 2778a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2878a6aeb1SAndreas Gohr */ 2978a6aeb1SAndreas Gohrfunction css_out(){ 3078a6aeb1SAndreas Gohr global $conf; 3178a6aeb1SAndreas Gohr global $lang; 3209edb711SAndreas Gohr global $config_cascade; 33bfd0f597STom N Harris global $INPUT; 3409edb711SAndreas Gohr 3529f2dfdcSAndreas Gohr if ($INPUT->str('s') == 'feed') { 366c47a78cSAnika Henke $mediatypes = array('feed'); 376c47a78cSAnika Henke $type = 'feed'; 386c47a78cSAnika Henke } else { 396c47a78cSAnika Henke $mediatypes = array('screen', 'all', 'print'); 406c47a78cSAnika Henke $type = ''; 41615960feSTom N Harris } 4278a6aeb1SAndreas Gohr 43*afb2c082SAndreas Gohr // decide from where to get the template 44bfd0f597STom N Harris $tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t'))); 45*afb2c082SAndreas Gohr if(!$tpl) $tpl = $conf['template']; 460e6f9f08SAnika Henke 4778a6aeb1SAndreas Gohr // The generated script depends on some dynamic options 48*afb2c082SAndreas Gohr $cache = new cache('styles'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tpl.$type,'.css'); 4978a6aeb1SAndreas Gohr 50*afb2c082SAndreas Gohr // load styl.ini 51*afb2c082SAndreas Gohr $styleini = css_styleini($tpl); 52*afb2c082SAndreas Gohr 53*afb2c082SAndreas Gohr print_r($styleini); 54519b3173SAndreas Gohr 55dbf794bfSMichael Hamann // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility 56dbf794bfSMichael Hamann if (isset($config_cascade['userstyle']['default'])) { 57dbf794bfSMichael Hamann $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default']; 58dbf794bfSMichael Hamann } 59dbf794bfSMichael Hamann 60*afb2c082SAndreas Gohr // cache influencers 61*afb2c082SAndreas Gohr $tplinc = tpl_basedir($tpl); 62*afb2c082SAndreas Gohr $cache_files = getConfigFiles('main'); 63*afb2c082SAndreas Gohr $cache_files[] = $tplinc.'style.ini'; 64*afb2c082SAndreas Gohr $cache_files[] = DOKU_CONF."tpl/$tpl/style.ini"; 65*afb2c082SAndreas Gohr $cache_files[] = __FILE__; 66*afb2c082SAndreas Gohr 6778a6aeb1SAndreas Gohr // Array of needed files and their web locations, the latter ones 6878a6aeb1SAndreas Gohr // are needed to fix relative paths in the stylesheets 6978a6aeb1SAndreas Gohr $files = array(); 7014977bd2SMichael Hamann foreach($mediatypes as $mediatype) { 7114977bd2SMichael Hamann $files[$mediatype] = array(); 72318cd03eSAnika Henke // load core styles 7314977bd2SMichael Hamann $files[$mediatype][DOKU_INC.'lib/styles/'.$mediatype.'.css'] = DOKU_BASE.'lib/styles/'; 7443576758SAndreas Gohr // load jQuery-UI theme 756c47a78cSAnika Henke if ($mediatype == 'screen') { 7614977bd2SMichael Hamann $files[$mediatype][DOKU_INC.'lib/scripts/jquery/jquery-ui-theme/smoothness.css'] = DOKU_BASE.'lib/scripts/jquery/jquery-ui-theme/'; 776c47a78cSAnika Henke } 78318cd03eSAnika Henke // load plugin styles 7914977bd2SMichael Hamann $files[$mediatype] = array_merge($files[$mediatype], css_pluginstyles($mediatype)); 80318cd03eSAnika Henke // load template styles 81*afb2c082SAndreas Gohr if (isset($styleini['stylesheets'][$mediatype])) { 82*afb2c082SAndreas Gohr $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets'][$mediatype]); 8309edb711SAndreas Gohr } 84318cd03eSAnika Henke // load user styles 85318cd03eSAnika Henke if(isset($config_cascade['userstyle'][$mediatype])){ 8614977bd2SMichael Hamann $files[$mediatype][$config_cascade['userstyle'][$mediatype]] = DOKU_BASE; 87318cd03eSAnika Henke } 88318cd03eSAnika Henke // load rtl styles 896c47a78cSAnika Henke // note: this adds the rtl styles only to the 'screen' media type 906c47a78cSAnika Henke // @deprecated 2012-04-09: rtl will cease to be a mode of its own, 916c47a78cSAnika Henke // please use "[dir=rtl]" in any css file in all, screen or print mode instead 92318cd03eSAnika Henke if ($mediatype=='screen') { 9378a6aeb1SAndreas Gohr if($lang['direction'] == 'rtl'){ 94*afb2c082SAndreas Gohr if (isset($styleini['stylesheets']['rtl'])) $files[$mediatype] = array_merge($files[$mediatype], $styleini['stylesheets']['rtl']); 95c5c68de9SMichael Hamann if (isset($config_cascade['userstyle']['rtl'])) $files[$mediatype][$config_cascade['userstyle']['rtl']] = DOKU_BASE; 9678a6aeb1SAndreas Gohr } 9778a6aeb1SAndreas Gohr } 9878a6aeb1SAndreas Gohr 9914977bd2SMichael Hamann $cache_files = array_merge($cache_files, array_keys($files[$mediatype])); 10014977bd2SMichael Hamann } 1016619f42eSAdrian Lang 10238f56bffSBen Coburn // check cache age & handle conditional request 1036619f42eSAdrian Lang // This may exit if a cache can be used 1046619f42eSAdrian Lang http_cached($cache->cache, 1056619f42eSAdrian Lang $cache->useCache(array('files' => $cache_files))); 10678a6aeb1SAndreas Gohr 1073899c2ecSMichael Hamann // start output buffering 1083899c2ecSMichael Hamann ob_start(); 1093899c2ecSMichael Hamann 1106c47a78cSAnika Henke // build the stylesheet 11114977bd2SMichael Hamann foreach ($mediatypes as $mediatype) { 11278a6aeb1SAndreas Gohr 113d15166e5SAndreas Gohr // print the default classes for interwiki links and file downloads 1146c47a78cSAnika Henke if ($mediatype == 'screen') { 115cacfb606SAnika Henke print '@media screen {'; 1161c2d1019SAndreas Gohr css_interwiki(); 117d15166e5SAndreas Gohr css_filetypes(); 118cacfb606SAnika Henke print '}'; 11978a6aeb1SAndreas Gohr } 12078a6aeb1SAndreas Gohr 1216c47a78cSAnika Henke // load files 1226c47a78cSAnika Henke $css_content = ''; 12314977bd2SMichael Hamann foreach($files[$mediatype] as $file => $location){ 12472a66eb7SAndreas Gohr $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 12572a66eb7SAndreas Gohr $css_content .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 1266c47a78cSAnika Henke $css_content .= css_loadfile($file, $location); 1276c47a78cSAnika Henke } 1286c47a78cSAnika Henke switch ($mediatype) { 1296c47a78cSAnika Henke case 'screen': 1306c47a78cSAnika Henke print NL.'@media screen { /* START screen styles */'.NL.$css_content.NL.'} /* /@media END screen styles */'.NL; 1316c47a78cSAnika Henke break; 1326c47a78cSAnika Henke case 'print': 1336c47a78cSAnika Henke print NL.'@media print { /* START print styles */'.NL.$css_content.NL.'} /* /@media END print styles */'.NL; 1346c47a78cSAnika Henke break; 1356c47a78cSAnika Henke case 'all': 1366c47a78cSAnika Henke case 'feed': 1376c47a78cSAnika Henke default: 1386c47a78cSAnika Henke print NL.'/* START rest styles */ '.NL.$css_content.NL.'/* END rest styles */'.NL; 1396c47a78cSAnika Henke break; 1406c47a78cSAnika Henke } 1416c47a78cSAnika Henke } 14278a6aeb1SAndreas Gohr // end output buffering and get contents 14378a6aeb1SAndreas Gohr $css = ob_get_contents(); 14478a6aeb1SAndreas Gohr ob_end_clean(); 14578a6aeb1SAndreas Gohr 1466e69c1baSAndreas Gohr // apply style replacements 147*afb2c082SAndreas Gohr $css = css_applystyle($css, $styleini['replacements']); 1486e69c1baSAndreas Gohr 14972a66eb7SAndreas Gohr // parse less 15072a66eb7SAndreas Gohr $css = css_parseless($css); 151d4a1ece8SAndreas Gohr 152d4a1ece8SAndreas Gohr // place all remaining @import statements at the top of the file 153f7d780b9SGabriel Birke $css = css_moveimports($css); 154f7d780b9SGabriel Birke 15578a6aeb1SAndreas Gohr // compress whitespace and comments 15678a6aeb1SAndreas Gohr if($conf['compress']){ 15778a6aeb1SAndreas Gohr $css = css_compress($css); 15878a6aeb1SAndreas Gohr } 15978a6aeb1SAndreas Gohr 160809d3ba5SAndreas Gohr // embed small images right into the stylesheet 161809d3ba5SAndreas Gohr if($conf['cssdatauri']){ 162809d3ba5SAndreas Gohr $base = preg_quote(DOKU_BASE,'#'); 163809d3ba5SAndreas Gohr $css = preg_replace_callback('#(url\([ \'"]*)('.$base.')(.*?(?:\.(png|gif)))#i','css_datauri',$css); 164809d3ba5SAndreas Gohr } 165809d3ba5SAndreas Gohr 1666619f42eSAdrian Lang http_cached_finish($cache->cache, $css); 16778a6aeb1SAndreas Gohr} 16878a6aeb1SAndreas Gohr 16978a6aeb1SAndreas Gohr/** 17072a66eb7SAndreas Gohr * Uses phpless to parse LESS in our CSS 17172a66eb7SAndreas Gohr * 17272a66eb7SAndreas Gohr * most of this function is error handling to show a nice useful error when 17372a66eb7SAndreas Gohr * LESS compilation fails 17472a66eb7SAndreas Gohr * 17572a66eb7SAndreas Gohr * @param $css 17672a66eb7SAndreas Gohr * @return string 17772a66eb7SAndreas Gohr */ 17872a66eb7SAndreas Gohrfunction css_parseless($css) { 17972a66eb7SAndreas Gohr $less = new lessc(); 18072a66eb7SAndreas Gohr try { 18172a66eb7SAndreas Gohr return $less->compile($css); 18272a66eb7SAndreas Gohr } catch(Exception $e) { 18372a66eb7SAndreas Gohr // get exception message 18472a66eb7SAndreas Gohr $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage()); 18572a66eb7SAndreas Gohr 18672a66eb7SAndreas Gohr // try to use line number to find affected file 18772a66eb7SAndreas Gohr if(preg_match('/line: (\d+)$/', $msg, $m)){ 18872a66eb7SAndreas Gohr $msg = substr($msg, 0, -1* strlen($m[0])); //remove useless linenumber 18972a66eb7SAndreas Gohr $lno = $m[1]; 19072a66eb7SAndreas Gohr 19172a66eb7SAndreas Gohr // walk upwards to last include 19272a66eb7SAndreas Gohr $lines = explode("\n", $css); 19372a66eb7SAndreas Gohr for($i=$lno-1; $i>=0; $i--){ 19472a66eb7SAndreas Gohr if(preg_match('/\/(\* XXXXXXXXX )(.*?)( XXXXXXXXX \*)\//', $lines[$i], $m)){ 19572a66eb7SAndreas Gohr // we found it, add info to message 19672a66eb7SAndreas Gohr $msg .= ' in '.$m[2].' at line '.($lno-$i); 19772a66eb7SAndreas Gohr break; 19872a66eb7SAndreas Gohr } 19972a66eb7SAndreas Gohr } 20072a66eb7SAndreas Gohr } 20172a66eb7SAndreas Gohr 20272a66eb7SAndreas Gohr // something went wrong 20372a66eb7SAndreas Gohr $error = 'A fatal error occured during compilation of the CSS files. '. 20472a66eb7SAndreas Gohr 'If you recently installed a new plugin or template it '. 20572a66eb7SAndreas Gohr 'might be broken and you should try disabling it again. ['.$msg.']'; 20672a66eb7SAndreas Gohr 20772a66eb7SAndreas Gohr echo ".dokuwiki:before { 20872a66eb7SAndreas Gohr content: '$error'; 20972a66eb7SAndreas Gohr background-color: red; 21072a66eb7SAndreas Gohr display: block; 21172a66eb7SAndreas Gohr background-color: #fcc; 21272a66eb7SAndreas Gohr border-color: #ebb; 21372a66eb7SAndreas Gohr color: #000; 21472a66eb7SAndreas Gohr padding: 0.5em; 21572a66eb7SAndreas Gohr }"; 21672a66eb7SAndreas Gohr 21772a66eb7SAndreas Gohr exit; 21872a66eb7SAndreas Gohr } 21972a66eb7SAndreas Gohr} 22072a66eb7SAndreas Gohr 22172a66eb7SAndreas Gohr/** 2226e69c1baSAndreas Gohr * Does placeholder replacements in the style according to 2236e69c1baSAndreas Gohr * the ones defined in a templates style.ini file 2246e69c1baSAndreas Gohr * 225d4a1ece8SAndreas Gohr * This also adds the ini defined placeholders as less variables 226d4a1ece8SAndreas Gohr * (sans the surrounding __ and with a ini_ prefix) 227d4a1ece8SAndreas Gohr * 2286e69c1baSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2296e69c1baSAndreas Gohr */ 230*afb2c082SAndreas Gohrfunction css_applystyle($css, $replacements) { 231cbe37079SAndreas Gohr // we convert ini replacements to LESS variable names 232cbe37079SAndreas Gohr // and build a list of variable: value; pairs 233d4a1ece8SAndreas Gohr $less = ''; 234*afb2c082SAndreas Gohr foreach((array) $replacements as $key => $value) { 235cbe37079SAndreas Gohr $lkey = trim($key, '_'); 236cbe37079SAndreas Gohr $lkey = '@ini_'.$lkey; 237cbe37079SAndreas Gohr $less .= "$lkey: $value;\n"; 238cbe37079SAndreas Gohr 239*afb2c082SAndreas Gohr $replacements[$key] = $lkey; 240d4a1ece8SAndreas Gohr } 241c51b334eSAndreas Gohr 242cbe37079SAndreas Gohr // we now replace all old ini replacements with LESS variables 243*afb2c082SAndreas Gohr $css = strtr($css, $replacements); 244cbe37079SAndreas Gohr 245cbe37079SAndreas Gohr // now prepend the list of LESS variables as the very first thing 246c51b334eSAndreas Gohr $css = $less.$css; 2476e69c1baSAndreas Gohr return $css; 2486e69c1baSAndreas Gohr} 2496e69c1baSAndreas Gohr 2506e69c1baSAndreas Gohr/** 251*afb2c082SAndreas Gohr * Load style ini contents 2520ac69508SAnika Henke * 253*afb2c082SAndreas Gohr * Loads and merges style.ini files from template and config and prepares 254*afb2c082SAndreas Gohr * the stylesheet modes 255*afb2c082SAndreas Gohr * 256*afb2c082SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 257*afb2c082SAndreas Gohr * @param string $tpl the used template 258*afb2c082SAndreas Gohr * @return array with keys 'stylesheets' and 'replacements' 2590e6f9f08SAnika Henke */ 260*afb2c082SAndreas Gohrfunction css_styleini($tpl) { 261*afb2c082SAndreas Gohr $stylesheets = array(); // mode, file => base 262*afb2c082SAndreas Gohr $replacements = array(); // placeholder => value 2630ac69508SAnika Henke 264*afb2c082SAndreas Gohr // load template's style.ini 265*afb2c082SAndreas Gohr $incbase = tpl_incdir($tpl); 266*afb2c082SAndreas Gohr $webbase = tpl_basedir($tpl); 267*afb2c082SAndreas Gohr $ini = $incbase.'style.ini'; 268*afb2c082SAndreas Gohr if(file_exists($ini)){ 269*afb2c082SAndreas Gohr $data = parse_ini_file($ini, true); 2700ac69508SAnika Henke 271*afb2c082SAndreas Gohr // stylesheets 272*afb2c082SAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 273*afb2c082SAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 274*afb2c082SAndreas Gohr } 275*afb2c082SAndreas Gohr 276*afb2c082SAndreas Gohr // replacements 277*afb2c082SAndreas Gohr if(is_array($data['replacements'])){ 278*afb2c082SAndreas Gohr $replacements = array_merge($replacements, $data['replacements']); 2790ac69508SAnika Henke } 2800ac69508SAnika Henke } 281*afb2c082SAndreas Gohr 282*afb2c082SAndreas Gohr // load configs's style.ini 283*afb2c082SAndreas Gohr $incbase = dirname($ini).'/'; 284*afb2c082SAndreas Gohr $webbase = DOKU_BASE; 285*afb2c082SAndreas Gohr $ini = DOKU_CONF."/tpl/$tpl/style.ini"; 286*afb2c082SAndreas Gohr if(file_exists($ini)){ 287*afb2c082SAndreas Gohr $data = parse_ini_file($ini, true); 288*afb2c082SAndreas Gohr 289*afb2c082SAndreas Gohr // stylesheets 290*afb2c082SAndreas Gohr if(is_array($data['stylesheets'])) foreach($data['stylesheets'] as $file => $mode){ 291*afb2c082SAndreas Gohr $stylesheets[$mode][$incbase.$file] = $webbase; 2920ac69508SAnika Henke } 293*afb2c082SAndreas Gohr 294*afb2c082SAndreas Gohr // replacements 295*afb2c082SAndreas Gohr if(is_array($data['replacements'])){ 296*afb2c082SAndreas Gohr $replacements = array_merge($replacements, $data['replacements']); 297*afb2c082SAndreas Gohr } 298*afb2c082SAndreas Gohr } 299*afb2c082SAndreas Gohr 300*afb2c082SAndreas Gohr return array( 301*afb2c082SAndreas Gohr 'stylesheets' => $stylesheets, 302*afb2c082SAndreas Gohr 'replacements' => $replacements 303*afb2c082SAndreas Gohr ); 3040e6f9f08SAnika Henke} 3050e6f9f08SAnika Henke 3060e6f9f08SAnika Henke/** 3071c2d1019SAndreas Gohr * Prints classes for interwikilinks 3081c2d1019SAndreas Gohr * 3091c2d1019SAndreas Gohr * Interwiki links have two classes: 'interwiki' and 'iw_$name>' where 3101c2d1019SAndreas Gohr * $name is the identifier given in the config. All Interwiki links get 3111c2d1019SAndreas Gohr * an default style with a default icon. If a special icon is available 3121c2d1019SAndreas Gohr * for an interwiki URL it is set in it's own class. Both classes can be 3131c2d1019SAndreas Gohr * overwritten in the template or userstyles. 3141c2d1019SAndreas Gohr * 3151c2d1019SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3161c2d1019SAndreas Gohr */ 3171c2d1019SAndreas Gohrfunction css_interwiki(){ 3181c2d1019SAndreas Gohr 3191c2d1019SAndreas Gohr // default style 3201c2d1019SAndreas Gohr echo 'a.interwiki {'; 3211c2d1019SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/interwiki.png) 0px 1px no-repeat;'; 3227b4ea081Smarklundeberg echo ' padding: 1px 0px 1px 16px;'; 3231c2d1019SAndreas Gohr echo '}'; 3241c2d1019SAndreas Gohr 3251c2d1019SAndreas Gohr // additional styles when icon available 3261c2d1019SAndreas Gohr $iwlinks = getInterwiki(); 3271c2d1019SAndreas Gohr foreach(array_keys($iwlinks) as $iw){ 3289d2ddea4SAndreas Gohr $class = preg_replace('/[^_\-a-z0-9]+/i','_',$iw); 3291c2d1019SAndreas Gohr if(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.png')){ 3309d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3311c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.png)'; 3321c2d1019SAndreas Gohr echo '}'; 3331c2d1019SAndreas Gohr }elseif(@file_exists(DOKU_INC.'lib/images/interwiki/'.$iw.'.gif')){ 3349d2ddea4SAndreas Gohr echo "a.iw_$class {"; 3351c2d1019SAndreas Gohr echo ' background-image: url('.DOKU_BASE.'lib/images/interwiki/'.$iw.'.gif)'; 3361c2d1019SAndreas Gohr echo '}'; 3371c2d1019SAndreas Gohr } 3381c2d1019SAndreas Gohr } 339d15166e5SAndreas Gohr} 3401c2d1019SAndreas Gohr 341d15166e5SAndreas Gohr/** 342d15166e5SAndreas Gohr * Prints classes for file download links 343d15166e5SAndreas Gohr * 344d15166e5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 345d15166e5SAndreas Gohr */ 346d15166e5SAndreas Gohrfunction css_filetypes(){ 347d15166e5SAndreas Gohr 348d15166e5SAndreas Gohr // default style 349035e07f1SKate Arzamastseva echo '.mediafile {'; 350d15166e5SAndreas Gohr echo ' background: transparent url('.DOKU_BASE.'lib/images/fileicons/file.png) 0px 1px no-repeat;'; 3515b77caf4SAndreas Gohr echo ' padding-left: 18px;'; 3525b77caf4SAndreas Gohr echo ' padding-bottom: 1px;'; 353d15166e5SAndreas Gohr echo '}'; 354d15166e5SAndreas Gohr 355d15166e5SAndreas Gohr // additional styles when icon available 35627bf7924STom N Harris // scan directory for all icons 35727bf7924STom N Harris $exts = array(); 35827bf7924STom N Harris if($dh = opendir(DOKU_INC.'lib/images/fileicons')){ 35927bf7924STom N Harris while(false !== ($file = readdir($dh))){ 36027bf7924STom N Harris if(preg_match('/([_\-a-z0-9]+(?:\.[_\-a-z0-9]+)*?)\.(png|gif)/i',$file,$match)){ 36127bf7924STom N Harris $ext = strtolower($match[1]); 36227bf7924STom N Harris $type = '.'.strtolower($match[2]); 36327bf7924STom N Harris if($ext!='file' && (!isset($exts[$ext]) || $type=='.png')){ 36427bf7924STom N Harris $exts[$ext] = $type; 365d15166e5SAndreas Gohr } 366d15166e5SAndreas Gohr } 3671c2d1019SAndreas Gohr } 36827bf7924STom N Harris closedir($dh); 36927bf7924STom N Harris } 37027bf7924STom N Harris foreach($exts as $ext=>$type){ 37127bf7924STom N Harris $class = preg_replace('/[^_\-a-z0-9]+/','_',$ext); 372035e07f1SKate Arzamastseva echo ".mf_$class {"; 37327bf7924STom N Harris echo ' background-image: url('.DOKU_BASE.'lib/images/fileicons/'.$ext.$type.')'; 37427bf7924STom N Harris echo '}'; 37527bf7924STom N Harris } 37627bf7924STom N Harris} 3771c2d1019SAndreas Gohr 3781c2d1019SAndreas Gohr/** 37978a6aeb1SAndreas Gohr * Loads a given file and fixes relative URLs with the 38078a6aeb1SAndreas Gohr * given location prefix 38178a6aeb1SAndreas Gohr */ 38278a6aeb1SAndreas Gohrfunction css_loadfile($file,$location=''){ 38378a6aeb1SAndreas Gohr if(!@file_exists($file)) return ''; 38478a6aeb1SAndreas Gohr $css = io_readFile($file); 38578a6aeb1SAndreas Gohr if(!$location) return $css; 38678a6aeb1SAndreas Gohr 387809d3ba5SAndreas Gohr $css = preg_replace('#(url\([ \'"]*)(?!/|data:|http://|https://| |\'|")#','\\1'.$location,$css); 388809d3ba5SAndreas Gohr $css = preg_replace('#(@import\s+[\'"])(?!/|data:|http://|https://)#', '\\1'.$location, $css); 389809d3ba5SAndreas Gohr 39078a6aeb1SAndreas Gohr return $css; 39178a6aeb1SAndreas Gohr} 39278a6aeb1SAndreas Gohr 393809d3ba5SAndreas Gohr/** 394809d3ba5SAndreas Gohr * Converte local image URLs to data URLs if the filesize is small 395809d3ba5SAndreas Gohr * 396809d3ba5SAndreas Gohr * Callback for preg_replace_callback 397809d3ba5SAndreas Gohr */ 398809d3ba5SAndreas Gohrfunction css_datauri($match){ 39928f4004cSAndreas Gohr global $conf; 40028f4004cSAndreas Gohr 401809d3ba5SAndreas Gohr $pre = unslash($match[1]); 402809d3ba5SAndreas Gohr $base = unslash($match[2]); 403809d3ba5SAndreas Gohr $url = unslash($match[3]); 404809d3ba5SAndreas Gohr $ext = unslash($match[4]); 405809d3ba5SAndreas Gohr 406809d3ba5SAndreas Gohr $local = DOKU_INC.$url; 407809d3ba5SAndreas Gohr $size = @filesize($local); 40828f4004cSAndreas Gohr if($size && $size < $conf['cssdatauri']){ 409809d3ba5SAndreas Gohr $data = base64_encode(file_get_contents($local)); 410809d3ba5SAndreas Gohr } 411809d3ba5SAndreas Gohr if($data){ 41288833bacSflammy $url = '\'data:image/'.$ext.';base64,'.$data.'\''; 413809d3ba5SAndreas Gohr }else{ 414809d3ba5SAndreas Gohr $url = $base.$url; 415809d3ba5SAndreas Gohr } 416809d3ba5SAndreas Gohr return $pre.$url; 417809d3ba5SAndreas Gohr} 418809d3ba5SAndreas Gohr 41915c394afSAndreas Gohr 42078a6aeb1SAndreas Gohr/** 42178a6aeb1SAndreas Gohr * Returns a list of possible Plugin Styles (no existance check here) 42278a6aeb1SAndreas Gohr * 42378a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 42478a6aeb1SAndreas Gohr */ 425318cd03eSAnika Henkefunction css_pluginstyles($mediatype='screen'){ 426208c0215SAndreas Gohr global $lang; 42778a6aeb1SAndreas Gohr $list = array(); 42878a6aeb1SAndreas Gohr $plugins = plugin_list(); 42978a6aeb1SAndreas Gohr foreach ($plugins as $p){ 430318cd03eSAnika Henke $list[DOKU_PLUGIN."$p/$mediatype.css"] = DOKU_BASE."lib/plugins/$p/"; 431d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/$mediatype.less"] = DOKU_BASE."lib/plugins/$p/"; 432318cd03eSAnika Henke // alternative for screen.css 433318cd03eSAnika Henke if ($mediatype=='screen') { 43478a6aeb1SAndreas Gohr $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 435d4a1ece8SAndreas Gohr $list[DOKU_PLUGIN."$p/style.less"] = DOKU_BASE."lib/plugins/$p/"; 43678a6aeb1SAndreas Gohr } 4376c47a78cSAnika Henke // @deprecated 2012-04-09: rtl will cease to be a mode of its own, 4386c47a78cSAnika Henke // please use "[dir=rtl]" in any css file in all, screen or print mode instead 439208c0215SAndreas Gohr if($lang['direction'] == 'rtl'){ 440208c0215SAndreas Gohr $list[DOKU_PLUGIN."$p/rtl.css"] = DOKU_BASE."lib/plugins/$p/"; 441208c0215SAndreas Gohr } 44278a6aeb1SAndreas Gohr } 44378a6aeb1SAndreas Gohr return $list; 44478a6aeb1SAndreas Gohr} 44578a6aeb1SAndreas Gohr 44678a6aeb1SAndreas Gohr/** 447f7d780b9SGabriel Birke * Move all @import statements in a combined stylesheet to the top so they 448f7d780b9SGabriel Birke * aren't ignored by the browser. 449f7d780b9SGabriel Birke * 450f7d780b9SGabriel Birke * @author Gabriel Birke <birke@d-scribe.de> 451f7d780b9SGabriel Birke */ 452f7d780b9SGabriel Birkefunction css_moveimports($css) 453f7d780b9SGabriel Birke{ 454f7d780b9SGabriel Birke if(!preg_match_all('/@import\s+(?:url\([^)]+\)|"[^"]+")\s*[^;]*;\s*/', $css, $matches, PREG_OFFSET_CAPTURE)) { 455f7d780b9SGabriel Birke return $css; 456f7d780b9SGabriel Birke } 457f7d780b9SGabriel Birke $newCss = ""; 458f7d780b9SGabriel Birke $imports = ""; 459f7d780b9SGabriel Birke $offset = 0; 460f7d780b9SGabriel Birke foreach($matches[0] as $match) { 461f7d780b9SGabriel Birke $newCss .= substr($css, $offset, $match[1] - $offset); 462f7d780b9SGabriel Birke $imports .= $match[0]; 463f7d780b9SGabriel Birke $offset = $match[1] + strlen($match[0]); 464f7d780b9SGabriel Birke } 465f7d780b9SGabriel Birke $newCss .= substr($css, $offset); 466f7d780b9SGabriel Birke return $imports.$newCss; 467f7d780b9SGabriel Birke} 468f7d780b9SGabriel Birke 469f7d780b9SGabriel Birke/** 47078a6aeb1SAndreas Gohr * Very simple CSS optimizer 47178a6aeb1SAndreas Gohr * 47278a6aeb1SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 47378a6aeb1SAndreas Gohr */ 47478a6aeb1SAndreas Gohrfunction css_compress($css){ 475fd7c2db0SAndreas Gohr //strip comments through a callback 476fd7c2db0SAndreas Gohr $css = preg_replace_callback('#(/\*)(.*?)(\*/)#s','css_comment_cb',$css); 477fd7c2db0SAndreas Gohr 478247c1c5dSAndreas Gohr //strip (incorrect but common) one line comments 479fd7c2db0SAndreas Gohr $css = preg_replace('/(?<!:)\/\/.*$/m','',$css); 480247c1c5dSAndreas Gohr 48178a6aeb1SAndreas Gohr // strip whitespaces 48278a6aeb1SAndreas Gohr $css = preg_replace('![\r\n\t ]+!',' ',$css); 483f5379589SChristopher Smith $css = preg_replace('/ ?([;,{}\/]) ?/','\\1',$css); 484f5379589SChristopher Smith $css = preg_replace('/ ?: /',':',$css); 48578a6aeb1SAndreas Gohr 48678a6aeb1SAndreas Gohr // shorten colors 48778a6aeb1SAndreas Gohr $css = preg_replace("/#([0-9a-fA-F]{1})\\1([0-9a-fA-F]{1})\\2([0-9a-fA-F]{1})\\3/", "#\\1\\2\\3",$css); 48878a6aeb1SAndreas Gohr 48978a6aeb1SAndreas Gohr return $css; 49078a6aeb1SAndreas Gohr} 49178a6aeb1SAndreas Gohr 492c00aef76SAndreas Gohr/** 493c00aef76SAndreas Gohr * Callback for css_compress() 494c00aef76SAndreas Gohr * 495c00aef76SAndreas Gohr * Keeps short comments (< 5 chars) to maintain typical browser hacks 496c00aef76SAndreas Gohr * 497c00aef76SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 498c00aef76SAndreas Gohr */ 499c00aef76SAndreas Gohrfunction css_comment_cb($matches){ 500c00aef76SAndreas Gohr if(strlen($matches[2]) > 4) return ''; 501c00aef76SAndreas Gohr return $matches[0]; 502c00aef76SAndreas Gohr} 50378a6aeb1SAndreas Gohr 504e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 505