NacoWiki PHP API

PluginCode
in package

Implements media handling for programming source code

This media handler handles programing languages source code. This is useful for storing code snippets in the wiki.

Source code meta-data can be stored in the file as comments:

For PHP:

##---
## title: my-php-file.php
## date: 2023-03-05
##---

For python:

##---
## title: sample-python.py
## date: 2023-03-25
##---

Adding additional languages

To add an additional language, create anew entry in the TYPES constant.

  • array-key : should be the main file extension for this file type.

Each array entry contains:

  • exts (optional) - array with additional file extensions
  • meta-re-start - regular expression used to match begining of metadata block
  • meta-re-end - regular expression used to match end of metadata block
  • meta-re-line - regular expression used to extract metadata line
  • `hl-js-class' - syntax highlighting class. Refer to Highlight.js
  • cm-mode - CodeMirror mode. See CodeMirror
  • cm-deps - Additional dependancies for CodeMirror.
  • template - Template to use to create new files.
Tags
todo

Adding additional languages: C/C++, sh/bash,batch, tcl/tk, perl, make, javascript, css go, rust, jinja2, lua, properties, sql, yaml

phpcod

PluginCode

link
https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md
link
https://github.com/codemirror/codemirror5/tree/master/mode

Table of Contents

Constants

TYPES  = ['yaml' => ['exts' => ['yaml', 'yml'], 'meta-re-start' => '/^\s*##---\s*\n/m', 'meta-re-end' => '/^\s*##---\s*\n/m', 'meta-re-line' => '/^\s*##\s*([^:]+):\s*(.*)$/', 'meta-re-log-line' => '/^\s*##\s*add-log:\s*(.*)$/m', 'hl-js-class' => 'language-yaml', 'cm-mode' => 'yaml', 'cm-deps' => ["mode/yaml/yaml.js"], 'template' => '---' . \PHP_EOL . '##---' . \PHP_EOL . '## title: {title}' . \PHP_EOL . '## date: {date}' . \PHP_EOL . '## author: {author}' . \PHP_EOL . '##---' . \PHP_EOL . '...' . \PHP_EOL . '' . \PHP_EOL], 'py' => ['meta-re-start' => '/^\s*##---\s*\n/m', 'meta-re-end' => '/^\s*##---\s*\n/m', 'meta-re-line' => '/^\s*##\s*([^:]+):\s*(.*)$/', 'meta-re-log-line' => '/^\s*##\s*add-log:\s*(.*)$/m', 'hl-js-class' => 'language-python', 'cm-mode' => 'python', 'cm-deps' => ["addon/edit/matchbrackets.js", "mode/python/python.js"], 'template' => '#!/usr/bin/env python3' . \PHP_EOL . '##---' . \PHP_EOL . '## title: {title}' . \PHP_EOL . '## date: {date}' . \PHP_EOL . '## author: {author}' . \PHP_EOL . '##---' . \PHP_EOL . '' . \PHP_EOL], 'php' => ['exts' => ['php', 'phps'], 'hl-js-class' => 'language-php', 'cm-mode' => 'application/x-httpd-php', 'cm-deps' => ["addon/edit/matchbrackets.js", "mode/htmlmixed/htmlmixed.js", "mode/xml/xml.js", "mode/javascript/javascript.js", "mode/css/css.js", "mode/clike/clike.js", "mode/php/php.js"], 'template' => '<?php' . \PHP_EOL . '##---' . \PHP_EOL . '## title: {title}' . \PHP_EOL . '## date: {date}' . \PHP_EOL . '## author: {author}' . \PHP_EOL . '##---' . \PHP_EOL . 'phpinfo();' . \PHP_EOL, 'skip-re' => '/<?php\s+/', 'meta-re-start' => '/^\s*##---\s*\n/m', 'meta-re-end' => '/^\s*##---\s*\n/m', 'meta-re-line' => '/^\s*##\s*([^:]+):\s*(.*)\s*$/', 'meta-re-log-line' => '/^\s*##\s*add-log:\s*(.*)$/m']]
VERSION  = '1.1'

Methods

edit()  : bool|null
Edit event handler
load()  : void
Loading entry point for this class
preSave()  : bool|null
preSave event handler
read()  : bool|null
Read event handler
readStruct()  : string
Read structured data
render()  : bool|null
Render event handler

Constants

TYPES

public mixed TYPES = ['yaml' => ['exts' => ['yaml', 'yml'], 'meta-re-start' => '/^\s*##---\s*\n/m', 'meta-re-end' => '/^\s*##---\s*\n/m', 'meta-re-line' => '/^\s*##\s*([^:]+):\s*(.*)$/', 'meta-re-log-line' => '/^\s*##\s*add-log:\s*(.*)$/m', 'hl-js-class' => 'language-yaml', 'cm-mode' => 'yaml', 'cm-deps' => ["mode/yaml/yaml.js"], 'template' => '---' . \PHP_EOL . '##---' . \PHP_EOL . '## title: {title}' . \PHP_EOL . '## date: {date}' . \PHP_EOL . '## author: {author}' . \PHP_EOL . '##---' . \PHP_EOL . '...' . \PHP_EOL . '' . \PHP_EOL], 'py' => ['meta-re-start' => '/^\s*##---\s*\n/m', 'meta-re-end' => '/^\s*##---\s*\n/m', 'meta-re-line' => '/^\s*##\s*([^:]+):\s*(.*)$/', 'meta-re-log-line' => '/^\s*##\s*add-log:\s*(.*)$/m', 'hl-js-class' => 'language-python', 'cm-mode' => 'python', 'cm-deps' => ["addon/edit/matchbrackets.js", "mode/python/python.js"], 'template' => '#!/usr/bin/env python3' . \PHP_EOL . '##---' . \PHP_EOL . '## title: {title}' . \PHP_EOL . '## date: {date}' . \PHP_EOL . '## author: {author}' . \PHP_EOL . '##---' . \PHP_EOL . '' . \PHP_EOL], 'php' => ['exts' => ['php', 'phps'], 'hl-js-class' => 'language-php', 'cm-mode' => 'application/x-httpd-php', 'cm-deps' => ["addon/edit/matchbrackets.js", "mode/htmlmixed/htmlmixed.js", "mode/xml/xml.js", "mode/javascript/javascript.js", "mode/css/css.js", "mode/clike/clike.js", "mode/php/php.js"], 'template' => '<?php' . \PHP_EOL . '##---' . \PHP_EOL . '## title: {title}' . \PHP_EOL . '## date: {date}' . \PHP_EOL . '## author: {author}' . \PHP_EOL . '##---' . \PHP_EOL . 'phpinfo();' . \PHP_EOL, 'skip-re' => '/<?php\s+/', 'meta-re-start' => '/^\s*##---\s*\n/m', 'meta-re-end' => '/^\s*##---\s*\n/m', 'meta-re-line' => '/^\s*##\s*([^:]+):\s*(.*)\s*$/', 'meta-re-log-line' => '/^\s*##\s*add-log:\s*(.*)$/m']]

Methods

edit()

Edit event handler

public static edit(NanoWikiApp $wiki, array<string|int, mixed> &$ev) : bool|null

Handles edit:[ext] events.

It creates a CodeMirror editor page with the configuration matching the source code language.

Parameters
$wiki : NanoWikiApp

running wiki instance

$ev : array<string|int, mixed>

passed event

Return values
bool|null

load()

Loading entry point for this class

public static load(array<string|int, mixed> $cfg) : void

Hooks media implemented by this class

Parameters
$cfg : array<string|int, mixed>

preSave()

preSave event handler

public static preSave(NanoWikiApp $wiki, array<string|int, mixed> &$ev) : bool|null

Handles preSave:[ext] events.

TODO: check for LOG lines and adds them to the props.

Parameters
$wiki : NanoWikiApp

running wiki instance

$ev : array<string|int, mixed>

passed event

Return values
bool|null

read()

Read event handler

public static read(NanoWikiApp $wiki, array<string|int, mixed> &$ev) : bool|null

Handles read:[ext] events.

Reads the source code and extract meta data tags.

Parameters
$wiki : NanoWikiApp

running wiki instance

$ev : array<string|int, mixed>

passed event

Return values
bool|null

readStruct()

Read structured data

public static readStruct(string $source, array<string|int, mixed> &$meta, string $ext) : string

Extracts meta data tags embedded in source code

Parameters
$source : string

text to process

$meta : array<string|int, mixed>

receives the read meta data

$ext : string

media file extension

Return values
string

Returns the source

render()

Render event handler

public static render(NanoWikiApp $wiki, array<string|int, mixed> &$ev) : bool|null

Handles render:[ext] events.

Escapes the source code and wraps it in an HTML block for syntax highlighting

Parameters
$wiki : NanoWikiApp

running wiki instance

$ev : array<string|int, mixed>

passed event

Return values
bool|null

        
On this page

Search results