The example will use the SMW::Parser::AfterLinksProcessingComplete
to process hash tags (e.g. #foo #foo_bar etc.) to add value annotations for the identified tag and the assigned property.
class HashTagParser {
private $subject;
private $property;
private $annotationProcessor;
private $showErrors = false;
public function __construct( AnnotationProcessor $annotationProcessor ) {
$this->annotationProcessor = $annotationProcessor;
}
public function setShowErrors( $showErrors ) {
$this->showErrors = $showErrors;
}
public function parse( $text, $property ) {
$semanticData = $this->annotationProcessor->getSemanticData();
$this->property = $property;
$this->subject = $semanticData->getSubject();
return preg_replace_callback( '/(?<!&|\S)#(\w+)/u', [ $this, 'process' ], $text );
}
public function process( array $matches ) {
$dataValue = $this->annotationProcessor->newDataValueByText(
$this->property,
$matches[1],
$matches[0],
$this->subject
);
if ( $this->annotationProcessor->canAnnotate() ) {
$this->annotationProcessor->getSemanticData()->addDataValue( $dataValue );
}
if ( ( $this->showErrors && !$dataValue->isValid() ) ) {
$result = str_replace( ':', ':', $result ) . $dataValue->getErrorText();
} else {
$result = $dataValue->getShortWikitext( true );
}
return $result;
}
}
\Hooks::register( 'SMW::Parser::AfterLinksProcessingComplete', function( &$text, AnnotationProcessor $annotationProcessor ) {
$hashTagParser = new HashTagParser(
$annotationProcessor
);
$property = 'Has keyword';
$text = $hashTagParser->parse( $text, $property );
return true;
} );