Skip to content

Instantly share code, notes, and snippets.

@schplurtz
Created October 12, 2018 10:10
Show Gist options
  • Save schplurtz/eae3763b73311b4ed8648e20ee462dd0 to your computer and use it in GitHub Desktop.
Save schplurtz/eae3763b73311b4ed8648e20ee462dd0 to your computer and use it in GitHub Desktop.
function to add CSS classname to DW pages baed on namespace, pagename, and namespace depth
/**
* compute a list of css class depending on $ID. there are
* CSS class depending on the pagename, the full pagename,
* the namespace and the full namespace, and the
* namespace tree level (root namespace is 0)
*
* Those classnames are meant to be used with 'userstyle.css'
* It would then be easy to create CSS for all start page.
* or all pages in the :archive namespace could have a special
* color.
*
* namespace classes are prefixed with dwn_. page classes
* prefixed with dwp_ and tree level are prefixed with
* dwl
* namespace separator ':' are converted to '_'
* '_' in id are doubled in CSS classname. Without this,
* both "page b in namespace a" 'a:b' and page 'a_b' would
* get the same css classname.
*
* examples :
* e => dwn_ dwn__ dwl_0 dwp_e
* _id_with_underscore => dwn_ dwn__ dwl_0 dwp___id__with__underscore
* c:d:e => dwn_c dwn_c_d dwn_d_ dwl_2 dwp_e dwp_c_d_e
* c:d_d:e => dwn_c dwn_c_d__d dwn_d__d_ dwl_2 dwp_e dwp_c_d__d_e
* a:b:c:d:e => dwn_a dwn_a_b dwn_a_b_c dwn_a_b_c_d dwn_d_ dwl_4 dwp_e dwp_a_b_c_d_e
*
* @author Schplurtz le Déboulonné
* @param string $id the full id of a page
* @return string space separated list of css class name
*/
function pageclass( $id ) {
if($id{0} == ':')
$id=substr($id,1);
$id=str_replace('_', '__', $id);
$chain = explode(':', $id);
$pgname = array_pop($chain);
$classes=Array();
$classes[]='dwl_'.count($chain);
$ns='dwn_';
$classes[]=$ns;
foreach($chain as $comp) {
$ns .= "_$comp";
$classes[]=$ns;
}
$parent=array_pop($chain);
$classes[]="dwn_{$parent}_";
$classes[]='dwp_'.$pgname;
if(!empty($parent))
$classes[]='dwp_'.str_replace(':', '_', $id);
return implode(' ', $classes );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment