Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Helper.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 2.83 KiB


001  <?php declare(strict_types=1);
002   
003  /**
004  * @package   s9e\TextFormatter
005  * @copyright Copyright (c) 2010-2022 The s9e authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\TextFormatter\Plugins\TaskLists;
009   
010  use s9e\TextFormatter\Parser;
011  use s9e\TextFormatter\Parser\Tag;
012   
013  class Helper
014  {
015      public static function filterListItem(Parser $parser, Tag $listItem, string $text): void
016      {
017          // Test whether the list item is followed by a task checkbox
018          $pos  = $listItem->getPos() + $listItem->getLen();
019          $pos += strspn($text, ' ', $pos);
020          $str  = substr($text, $pos, 3);
021          if (!preg_match('/\\[[ Xx]\\]/', $str))
022          {
023              return;
024          }
025   
026          // Create a tag for the task and assign it a random ID
027          $taskId    = uniqid();
028          $taskState = ($str === '[ ]') ? 'unchecked' : 'checked';
029   
030          $task = $parser->addSelfClosingTag('TASK', $pos, 3);
031          $task->setAttribute('id',    $taskId);
032          $task->setAttribute('state', $taskState);
033   
034          $listItem->cascadeInvalidationTo($task);
035      }
036   
037      /**
038      * Return stats from a parsed representation
039      *
040      * @param  string $xml Parsed XML
041      * @return array       Number of "checked" and "unchecked" tasks
042      */
043      public static function getStats(string $xml): array
044      {
045          $stats = ['checked' => 0, 'unchecked' => 0];
046   
047          preg_match_all('((?<=<)TASK(?: [^=]++="[^"]*+")*? state="\\K\\w++)', $xml, $m);
048          foreach ($m[0] as $state)
049          {
050              if (!isset($stats[$state]))
051              {
052                  $stats[$state] = 0;
053              }
054              ++$stats[$state];
055          }
056   
057          return $stats;
058      }
059   
060      /**
061      * Mark given task checked in XML
062      *
063      * @param  string $xml Parsed XML
064      * @param  string $id  Task's ID
065      * @return string      Updated XML
066      */
067      public static function checkTask(string $xml, string $id): string
068      {
069          return self::setTaskState($xml, $id, 'checked', 'x');
070      }
071   
072      /**
073      * Mark given task unchecked in XML
074      *
075      * @param  string $xml Parsed XML
076      * @param  string $id  Task's ID
077      * @return string      Updated XML
078      */
079      public static function uncheckTask(string $xml, string $id): string
080      {
081          return self::setTaskState($xml, $id, 'unchecked', ' ');
082      }
083   
084      /**
085      * Change the state and marker of given task in XML
086      *
087      * @param  string $xml    Parsed XML
088      * @param  string $id     Task's ID
089      * @param  string $state  Task's state ("checked" or "unchecked")
090      * @param  string $marker State marker ("x" or " ")
091      * @return string         Updated XML
092      */
093      protected static function setTaskState(string $xml, string $id, string $state, string $marker): string
094      {
095          return preg_replace_callback(
096              '((?<=<)TASK(?: [^=]++="[^"]*+")*? id="' . preg_quote($id) . '"\\K([^>]*+)>[^<]*+(?=</TASK>))',
097              function ($m) use ($state, $marker)
098              {
099                  preg_match_all('( ([^=]++)="[^"]*+")', $m[1], $m);
100   
101                  $attributes          = array_combine($m[1], $m[0]);
102                  $attributes['state'] = ' state="' . $state . '"';
103                  ksort($attributes);
104   
105                  return implode('', $attributes) . '>[' . $marker . ']';
106              },
107              $xml
108          );
109      }
110  }