Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from Wikimedia-AU/updates
Browse files Browse the repository at this point in the history
Add multiple card syntax, and upgrade deps etc.
  • Loading branch information
samwilson authored Sep 23, 2021
2 parents 1d745f3 + e3ddffd commit 5138ff6
Show file tree
Hide file tree
Showing 6 changed files with 789 additions and 904 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches:
- main
- dev
pull_request:
branches:
- '**'

jobs:
build:

strategy:
matrix:
# All supported PHP versions https://www.php.net/supported-versions.php
php: [ '7.3', '7.4', '8.0' ]

runs-on: 'ubuntu-latest'

name: PHP ${{ matrix.php }}

steps:
- name: Checkout
uses: actions/checkout@v1

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{matrix.php}}

- name: Install
run: |
composer install
npm ci
- name: Test
run: |
composer test
npm test
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
"issues": "https://phabricator.wikimedia.org/tag/link-cards",
"source": "https://gerrit.wikimedia.org/r/mediawiki/extensions/LinkCards"
},
"require": {
"symfony/polyfill-php80": "^1.0"
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "36.0.0",
"mediawiki/mediawiki-codesniffer": "37.0.0",
"mediawiki/minus-x": "1.1.1",
"php-parallel-lint/php-console-highlighter": "0.5.0",
"php-parallel-lint/php-parallel-lint": "1.3.0"
"php-parallel-lint/php-parallel-lint": "1.3.1"
},
"scripts": {
"test": [
Expand Down
87 changes: 64 additions & 23 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,73 @@ class Hooks implements ParserFirstCallInitHook {
* @return bool|void True or no return value to continue or false to abort
*/
public function onParserFirstCallInit( $parser ) {
$parser->setFunctionHook( 'linkcard', [ $this, 'render' ] );
$parser->setFunctionHook( 'linkcards', [ $this, 'renderCards' ] );
$parser->setFunctionHook( 'linkcard', [ $this, 'renderCard' ] );
}

/**
* @param Parser $parser
* @return array|string
* @return mixed[]
*/
public function renderCards( Parser $parser ) {
$args = $this->parseArgs( func_get_args(), [] );
$cardNum = 1;
$html = '';
while ( isset( $args[ 'link' . $cardNum ] ) ) {
$cardArgs = [
'link' => $args[ 'link' . $cardNum ],
'title' => $args[ 'title' . $cardNum ] ?? false,
'body' => $args[ 'body' . $cardNum ] ?? false,
'image' => $args[ 'image' . $cardNum ] ?? false,
'image-width' => $args[ 'image-width' . $cardNum ] ?? 300,
'image-offset-dir' => $args[ 'image-offset-dir' . $cardNum ] ?? false,
'image-offset-val' => $args[ 'image-offset-val' . $cardNum ] ?? false,
];
$html .= $this->getCard( $cardArgs );
$cardNum++;
}
return $this->addModuleGetOutput( $parser, $html );
}

/**
* @param Parser $parser
* @return mixed[]
*/
public function renderCard( Parser $parser ) {
$args = $this->parseArgs( func_get_args(), [
'title' => false,
'body' => false,
'image' => false,
'image-width' => 300,
'image-offset-dir' => false,
'image-offset-val' => false,
] );
return $this->addModuleGetOutput( $parser, $this->getCard( $args ) );
}

/**
* @param Parser $parser
* @param string $html
* @return mixed[]
*/
public function render( Parser $parser ) {
$args = $this->parseArgs( func_get_args() );
private function addModuleGetOutput( Parser $parser, $html ) {
$parser->getOutput()->addModuleStyles( 'ext.LinkCards' );
return [
0 => Html::rawElement( 'div', [ 'class' => 'ext-linkcards' ], $html ),
'isHTML' => true,
];
}

/**
* @param string[] $args
* @return string
*/
private function getCard( array $args ): string {
// Title.
$title = false;
if ( $args['title'] ) {
$title = Html::element( 'span', [ 'class' => 'ext-linkcards-title' ], $args['title'] );
}

// Link and main.
$link = $args['link'];
if ( !str_starts_with( $link, 'http' ) ) {
Expand All @@ -45,20 +96,16 @@ public function render( Parser $parser ) {
}
$main = Html::rawElement( 'span', [ 'class' => 'ext-linkcards-main' ], $title . ' ' . $args['body'] );
$anchor = Html::rawElement( 'a', $anchorParams, $this->getImageHtml( $args ) . ' ' . $main );

// Main output.
$parser->getOutput()->addModuleStyles( 'ext.LinkCards' );
return [
0 => Html::rawElement( 'div', [ 'class' => 'ext-linkcards-card' ], $anchor ),
'isHTML' => true,
];
$card = Html::rawElement( 'div', [ 'class' => 'ext-linkcards-card' ], $anchor );
return $card;
}

/**
* @param string[] $args
* @param string[] $argsDefaults
* @return mixed[]
*/
private function parseArgs( $args ): array {
private function parseArgs( $args, $argsDefaults ): array {
$options = array_slice( $args, 1 );
$link = isset( $options[0] ) && $options[0] !== '' ? $options[0] : '';
unset( $options[0] );
Expand All @@ -72,16 +119,7 @@ private function parseArgs( $args ): array {
$argsSupplied[ $pair[0] ] = true;
}
}
$argsDefaults = [
'link' => $link,
'title' => false,
'body' => false,
'image' => false,
'image-width' => 300,
'image-offset-dir' => false,
'image-offset-val' => false,
];
return array_merge( $argsDefaults, $argsSupplied );
return array_merge( [ 'link' => $link ], $argsDefaults, $argsSupplied );
}

/**
Expand All @@ -100,6 +138,9 @@ private function getImageHtml( $args ): string {
return '';
}
$mediaTransformOutput = $file->transform( [ 'width' => $args['image-width'] ] );
if ( $mediaTransformOutput === false ) {
return 'Unable to transform ' . $file->getTitle()->getDBkey();
}
$style = '';
if (
in_array( $args['image-offset-dir'], [ 'top', 'left', 'bottom', 'right' ] )
Expand Down
Loading

0 comments on commit 5138ff6

Please sign in to comment.