Skip to content

Instantly share code, notes, and snippets.

@jdlrobson
Last active December 16, 2015 13:08
Show Gist options
  • Save jdlrobson/5439509 to your computer and use it in GitHub Desktop.
Save jdlrobson/5439509 to your computer and use it in GitHub Desktop.
Using a hook to do mobile formatting on section level basis
diff --git a/MobileFrontend.php b/MobileFrontend.php
index 55f8718..2a83d33 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -107,6 +107,7 @@ $wgHooks['UserLoginComplete'][] = 'MobileFrontendHooks::onUserLoginComplete';
$wgHooks['UserLoginForm'][] = 'MobileFrontendHooks::onUserLoginForm';
$wgHooks['UserCreateForm'][] = 'MobileFrontendHooks::onUserCreateForm';
$wgHooks['BeforePageDisplay'][] = 'MobileFrontendHooks::onBeforePageDisplay';
+$wgHooks['OutputPageParserOutput'][] = 'MobileFrontendHooks::onOutputPageParserOutput';
$wgSpecialPages['Uploads'] = 'SpecialUploads';
$wgSpecialPages['MobileDiff'] = 'SpecialMobileDiff';
@@ -344,7 +345,9 @@ $wgResourceModules['mobile.toggling'] = $wgMFMobileResourceBoilerplate + array(
'mobile-frontend-show-button',
'mobile-frontend-hide-button',
),
- 'styles' => array(),
+ 'styles' => array(
+ 'stylesheets/modules/mf-toggle.css',
+ ),
'scripts' => array(
'javascripts/modules/mf-toggle.js',
),
@@ -429,7 +432,6 @@ $wgResourceModules['mobile.alpha'] = $wgMFMobileResourceBoilerplate + array(
$wgResourceModules['mobile.stable.styles'] = $wgMFMobileResourceBoilerplate + array(
'styles' => array(
'stylesheets/modules/mf-search.css',
- 'stylesheets/modules/mf-toggle.css',
'stylesheets/modules/mf-references.css',
'stylesheets/modules/mf-cleanuptemplates.css',
'stylesheets/modules/mf-watchstar.css',
diff --git a/includes/MobileFormatter.php b/includes/MobileFormatter.php
index 8cf3c57..ae070e6 100644
--- a/includes/MobileFormatter.php
+++ b/includes/MobileFormatter.php
@@ -33,7 +33,6 @@ class MobileFormatter extends HtmlFormatter {
'#wpPreview', // on action=edit preview button currently does not work in mobile
'div.sister-project',
'div.magnify',
- '.editsection',
'span.t',
'.portal',
'#protected-icon',
diff --git a/includes/MobileFrontend.hooks.php b/includes/MobileFrontend.hooks.php
index 4e37355..37ef002 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -10,6 +10,38 @@
class MobileFrontendHooks {
+ public static function onOutputPageParserOutput( $out, $parserOutput ) {
+ $title = $out->getTitle();
+ function filterContent( $content, $title ) {
+ $f = new MobileFormatter( HtmlFormatter::wrapHTML( $content ), $title, 'HTML' );
+ $f->remove( array( '#toc' ) );
+ $f->filterContent();
+ return $f->getText();
+ }
+
+ $skin = $out->getSkin()->skinname;
+ $action = $out->getRequest()->getVal( 'action' );
+ if ( $action && $action !== 'view' ) {
+ return true;
+ } else if ( $skin === 'minerva' || $skin === 'mobile' ) { // FIXME: need for two skin names
+ $sections = $parserOutput->getSections();
+ $html = $parserOutput->getText();
+ $chunks = preg_split( '/<h(?=[1-6]\b)/i', $html );
+ $out->setProperty( 'lead', filterContent( $chunks[0] . '<h'.$chunks[1], $title ) ); // table of contents has an h2 in it
+ $out->setProperty( 'toc', $parserOutput->getTOCHTML() );
+ foreach ( $sections as $key => $section ) {
+ $sections[$key]['text'] = filterContent(
+ trim(
+ preg_replace( '#<(h[1-6])\b.*?<\s*/\s*\\1>#', '',
+ '<h' . $chunks[ $key + 2 ] ) // return the h that we split on, remove heading from output.
+ ),
+ $title
+ );
+ }
+ $out->setProperty( 'sections', $sections );
+ }
+ return true;
+ }
/**
* MakeGlobalVariablesScript hook handler
diff --git a/includes/skins/MinervaTemplate.php b/includes/skins/MinervaTemplate.php
index 87b13bc..86978e7 100644
--- a/includes/skins/MinervaTemplate.php
+++ b/includes/skins/MinervaTemplate.php
@@ -120,6 +120,51 @@ HTML;
}
}
+ private function renderContent( $data ) {
+ if ( isset( $data['sections'] ) ) {
+ echo '<div id="content_0">';
+ echo $data['lead'];
+ echo '</div>';
+ $id = 1;
+ foreach( $data['sections'] as $section ):
+ $isH2 = $section['level'] === '2';
+ $heading = Html::element( 'h' . $section['level'],
+ array(
+ 'class' => 'section_heading',
+ 'id' => 'section_' . $id,
+ 'anchor' => $section['anchor'],
+ ),
+ $section['line']
+ );
+
+ $content = Html::openElement( 'div',
+ array(
+ 'class' => 'content_block',
+ 'id' => 'content_' . $id,
+ )
+ );
+ if ( $isH2 ) {
+ if ( $id > 1 ) {
+ echo '</div>'; // close first content
+ echo '</div>'; // close first section
+ }
+ echo '<div class="section">';
+ echo $heading;
+ echo $content;
+ echo $section['text'];
+ } else {
+ echo $heading;
+ echo $section['text'];
+ }
+ $id += 1;
+ endforeach;
+ echo '</div>'; // close last content
+ echo '</div>'; // close last section
+ } else {
+ echo $data['bodytext'];
+ }
+ }
+
private function render( $data ) { // FIXME: replace with template engines
$languages = $this->getLanguages();
$variants = $this->getLanguageVariants();
@@ -171,7 +216,7 @@ HTML;
</ul>
</div>
<?php
- echo $data[ 'bodytext' ];
+ echo $this->renderContent( $data );
echo $this->renderLanguages( $languageData );
echo $data['postbodytext'];
?>
diff --git a/includes/skins/SkinMinerva.php b/includes/skins/SkinMinerva.php
index ff0841b..7d05d74 100644
--- a/includes/skins/SkinMinerva.php
+++ b/includes/skins/SkinMinerva.php
@@ -22,13 +22,17 @@ class SkinMinerva extends SkinTemplate {
'mobile.stable.universal',
'mobile.stable.dependencies',
'minerva.scripts',
+ 'mobile.toggling',
+ 'mobile.toggling.styles',
);
$out->addModules( $modules );
-
}
- protected function prepareTemplate() {
- $tpl = parent::prepareTemplate();
+ public function setupTemplate( $classname, $repository = false, $cache_dir = false ) {
+ $tpl = new $classname();
+ $tpl->set( 'sections', $this->getOutput()->getProperty( 'sections' ) );
+ $tpl->set( 'lead', $this->getOutput()->getProperty( 'lead' ) );
+ $tpl->set( 'toc', $this->getOutput()->getProperty( 'toc' ) );
return $tpl;
}
@dantman
Copy link

dantman commented Apr 23, 2013

ParserOutput::setProperty is for stuff that's supposed to go into the database.

@jdlrobson
Copy link
Author

setProperty is on OutputPage not ParserOutput so it doesn't get stored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment