Plugin Directory

Changeset 3213384


Ignore:
Timestamp:
12/26/2024 03:02:12 PM (15 months ago)
Author:
bitspecter
Message:

Fix regex bugs, add new regex rules, improve cache

Location:
pevne-mezery
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • pevne-mezery/tags/1.3/includes/class-cache-handler.php

    r3162288 r3213384  
    1313     * @return string The unique cache key.
    1414     */
    15     private static function generate_cache_key(string $content): string
     15    private static function generate_cache_key(string $content, string $context = 'default'): string
    1616    {
    17         return 'fs_fixed_spaces_' . md5($content);
     17        return 'fs_fixed_spaces_' . $context . '_' . md5($content);
    1818    }
    1919
     
    7070    {
    7171        $content = get_post_field('post_content', $post_id);
     72
    7273        if ($content) {
    7374            self::delete_cached_content($content);
    7475        }
    7576    }
     77
     78    /**
     79     * Deletes all cache entries for a specific context.
     80     *
     81     * @param string $context The context to delete cache for.
     82     * @return void
     83     */
     84    public static function delete_cache_by_context(string $context): void
     85    {
     86        global $wpdb;
     87        $prefix = 'fs_fixed_spaces_' . $context;
     88        $wpdb->query(
     89            $wpdb->prepare(
     90                "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
     91                '_transient_' . $prefix . '%'
     92            )
     93        );
     94    }
    7695}
  • pevne-mezery/tags/1.3/includes/class-content-handler.php

    r3164416 r3213384  
    55class ContentHandler
    66{
     7    /**
     8     * Debug mode flag. If true, replaces non-breaking spaces with a visible symbol.
     9     *
     10     * @var bool
     11     */
     12    private static $debug_mode = false;
     13
     14    /**
     15     * Toggle debug mode.
     16     *
     17     * @param bool $enabled Enable or disable debug mode.
     18     * @return void
     19     */
     20    public static function set_debug_mode(bool $enabled): void
     21    {
     22        self::$debug_mode = $enabled;
     23    }
     24
    725    /**
    826     * Main function to process the content and apply typographical rules.
     
    1331    public static function process_content(string $content): string
    1432    {
     33        // Check if content is already cached
     34        $cached_content = CacheHandler::get_cached_content($content);
     35       
     36        if ($cached_content !== null) {
     37            return $cached_content;
     38        }
     39
    1540        // Split content into HTML and text parts
    1641        $text_parts = preg_split('/(<[^>]+>)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
     
    2045            if (!preg_match('/^<.*>$/', $part)) {
    2146                foreach (self::get_regex_rules() as $pattern => $replacement) {
     47                    $replacement = self::$debug_mode
     48                        ? str_replace('&nbsp;', '⭕️', $replacement)
     49                        : $replacement;
    2250                    $part = preg_replace($pattern, $replacement, $part);
    2351                }
     
    2553        }
    2654
    27         return implode('', $text_parts);
     55        $processed_content = implode('', $text_parts);
     56
     57        // Save processed content to cache
     58        CacheHandler::save_cached_content($content, $processed_content);
     59
     60        return $processed_content;
    2861    }
    2962
     
    3669    private static function get_regex_rules(): array
    3770    {
    38         // Using combined regex for similar rules and more efficient replacements
    3971        return [
    40             // Math expressions with non-breakable spaces
     72            // Matematické výrazy s pevnými mezerami
    4173            '/(\d)\s+([+\-*\/=])\s+(\d)/u' => '$1&nbsp;$2&nbsp;$3',
    4274
    43             // Single-character prepositions and conjunctions
     75            // Jednopísmenné předložky a spojky
    4476            '/\b(k|s|v|z|o|u|a|i)\s+/iu' => '$1&nbsp;',
    4577
    46             '/\s*–\s*/u' => '&nbsp;–&nbsp;', // Specific rule for en dash
     78            // Pomlčka s pevnými mezerami
     79            '/\s*–\s*/u' => '&nbsp;–&nbsp;',
     80            '/(\d+)\s*–\s*(\d+)/u' => '$1–$2',
    4781
    48             // Units of measurement
    49             '/(\d+)\s+(h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|m\/s|km\/h|°|°C|°F|Kč|€|\$|%|dní|lidí)/u' => '$1&nbsp;$2',
     82            // Jednotky a složené výrazy
     83            '/(\d+)\s+(l|h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|kW|W|m\/s|km\/h|l\/\d+|°|°C|°F|Kč|€|\$|%|dní|lidí|kg)/u' => '$1&nbsp;$2',
    5084
    51             // Czech abbreviations 
    52             '/\b(Bc|Mgr|Ing|Ph\.D|LL\.B|MUDr|JUDr|prof|voj|čet|rtm|por|kpt|plk|gen|Dr|doc|cca|č|čís|čj|čp|fa|fě|fy|kupř|mj|např|p|pí|popř|př|přib|přibl|sl|str|sv|tj|tzn|tzv|zvl)\.\s+/u' => '$1.&nbsp;',
     85            // Pořadová čísla a zkratky
     86            '/(\d+)\s*([%|kg])/u' => '$1&nbsp;$2',
    5387
    54             // Space after ordered number
    55             '/(\d+\.)\s+([0-9a-záčďéěíňóřšťúýž])/u' => '$1&nbsp;$2', // After ordinal numbers
     88            // Zkratky (např., apod., atd.)
     89            '/\b(např|atd|apod|tj|tzn|tzv|mj|cca|vs|resp|ap|fa|č|čj|čp|čís|kupř|mj|tj|tj.|tzn|tzv)\.\s+/u' => '$1.&nbsp;',
     90
     91            // Tituly (např. JUDr., Ing., Mgr.)
     92            '/\b(JUDr|Ph\.D|LL\.B|MUDr|Mgr|Bc|Ing|CSc|Th\.D|MBA|DiS|prof|doc|RNDr|PhDr|PaedDr|ThLic|Dr|BcA|MgA|PharmDr|MVDr|JUDr|ThDr|Ph\.Mr|prof|etc)\.\s+/u' => '$1.&nbsp;',
     93            '/(\d+\.)\s+(\S)/u' => '$1&nbsp;$2',
     94
     95            // Úhlové stupně, minuty, vteřiny
     96            '/(\d+)(\s*)(°|\'|\")/u' => '$1&nbsp;$3',
     97
     98            // Lomítka
     99            '/\s+\/\s+/u' => ' / ',
     100            '/([a-zA-Z])\/([a-zA-Z])/u' => '$1/$2',
     101
     102            // Exponenty a indexy
     103            '/(\d+)([²³])/u' => '$1$2',
     104
     105            // Tři tečky
     106            '/(\S)\s*\.{3}/u' => '$1&hellip;',
     107            '/\.{3}\s+/u' => '&hellip;&nbsp;',
     108            '/\s+\.{3}\s+/u' => '&nbsp;&hellip;&nbsp;',
     109            '/(\S)…(\s|&nbsp;)/u' => '$1&hellip;&nbsp;',
     110            '/\s+…\s+/u' => '&nbsp;&hellip;&nbsp;',
     111            '/…(\s|&nbsp;)/u' => '&hellip;&nbsp;',
     112
     113            // §
     114            '/\s+§\s+/u' => '&nbsp;§&nbsp;',
    56115        ];
    57116    }
  • pevne-mezery/trunk/includes/class-cache-handler.php

    r3162288 r3213384  
    1313     * @return string The unique cache key.
    1414     */
    15     private static function generate_cache_key(string $content): string
     15    private static function generate_cache_key(string $content, string $context = 'default'): string
    1616    {
    17         return 'fs_fixed_spaces_' . md5($content);
     17        return 'fs_fixed_spaces_' . $context . '_' . md5($content);
    1818    }
    1919
     
    7070    {
    7171        $content = get_post_field('post_content', $post_id);
     72
    7273        if ($content) {
    7374            self::delete_cached_content($content);
    7475        }
    7576    }
     77
     78    /**
     79     * Deletes all cache entries for a specific context.
     80     *
     81     * @param string $context The context to delete cache for.
     82     * @return void
     83     */
     84    public static function delete_cache_by_context(string $context): void
     85    {
     86        global $wpdb;
     87        $prefix = 'fs_fixed_spaces_' . $context;
     88        $wpdb->query(
     89            $wpdb->prepare(
     90                "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
     91                '_transient_' . $prefix . '%'
     92            )
     93        );
     94    }
    7695}
  • pevne-mezery/trunk/includes/class-content-handler.php

    r3164416 r3213384  
    55class ContentHandler
    66{
     7    /**
     8     * Debug mode flag. If true, replaces non-breaking spaces with a visible symbol.
     9     *
     10     * @var bool
     11     */
     12    private static $debug_mode = false;
     13
     14    /**
     15     * Toggle debug mode.
     16     *
     17     * @param bool $enabled Enable or disable debug mode.
     18     * @return void
     19     */
     20    public static function set_debug_mode(bool $enabled): void
     21    {
     22        self::$debug_mode = $enabled;
     23    }
     24
    725    /**
    826     * Main function to process the content and apply typographical rules.
     
    1331    public static function process_content(string $content): string
    1432    {
     33        // Check if content is already cached
     34        $cached_content = CacheHandler::get_cached_content($content);
     35       
     36        if ($cached_content !== null) {
     37            return $cached_content;
     38        }
     39
    1540        // Split content into HTML and text parts
    1641        $text_parts = preg_split('/(<[^>]+>)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
     
    2045            if (!preg_match('/^<.*>$/', $part)) {
    2146                foreach (self::get_regex_rules() as $pattern => $replacement) {
     47                    $replacement = self::$debug_mode
     48                        ? str_replace('&nbsp;', '⭕️', $replacement)
     49                        : $replacement;
    2250                    $part = preg_replace($pattern, $replacement, $part);
    2351                }
     
    2553        }
    2654
    27         return implode('', $text_parts);
     55        $processed_content = implode('', $text_parts);
     56
     57        // Save processed content to cache
     58        CacheHandler::save_cached_content($content, $processed_content);
     59
     60        return $processed_content;
    2861    }
    2962
     
    3669    private static function get_regex_rules(): array
    3770    {
    38         // Using combined regex for similar rules and more efficient replacements
    3971        return [
    40             // Math expressions with non-breakable spaces
     72            // Matematické výrazy s pevnými mezerami
    4173            '/(\d)\s+([+\-*\/=])\s+(\d)/u' => '$1&nbsp;$2&nbsp;$3',
    4274
    43             // Single-character prepositions and conjunctions
     75            // Jednopísmenné předložky a spojky
    4476            '/\b(k|s|v|z|o|u|a|i)\s+/iu' => '$1&nbsp;',
    4577
    46             '/\s*–\s*/u' => '&nbsp;–&nbsp;', // Specific rule for en dash
     78            // Pomlčka s pevnými mezerami
     79            '/\s*–\s*/u' => '&nbsp;–&nbsp;',
     80            '/(\d+)\s*–\s*(\d+)/u' => '$1–$2',
    4781
    48             // Units of measurement
    49             '/(\d+)\s+(h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|m\/s|km\/h|°|°C|°F|Kč|€|\$|%|dní|lidí)/u' => '$1&nbsp;$2',
     82            // Jednotky a složené výrazy
     83            '/(\d+)\s+(l|h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|kW|W|m\/s|km\/h|l\/\d+|°|°C|°F|Kč|€|\$|%|dní|lidí|kg)/u' => '$1&nbsp;$2',
    5084
    51             // Czech abbreviations 
    52             '/\b(Bc|Mgr|Ing|Ph\.D|LL\.B|MUDr|JUDr|prof|voj|čet|rtm|por|kpt|plk|gen|Dr|doc|cca|č|čís|čj|čp|fa|fě|fy|kupř|mj|např|p|pí|popř|př|přib|přibl|sl|str|sv|tj|tzn|tzv|zvl)\.\s+/u' => '$1.&nbsp;',
     85            // Pořadová čísla a zkratky
     86            '/(\d+)\s*([%|kg])/u' => '$1&nbsp;$2',
    5387
    54             // Space after ordered number
    55             '/(\d+\.)\s+([0-9a-záčďéěíňóřšťúýž])/u' => '$1&nbsp;$2', // After ordinal numbers
     88            // Zkratky (např., apod., atd.)
     89            '/\b(např|atd|apod|tj|tzn|tzv|mj|cca|vs|resp|ap|fa|č|čj|čp|čís|kupř|mj|tj|tj.|tzn|tzv)\.\s+/u' => '$1.&nbsp;',
     90
     91            // Tituly (např. JUDr., Ing., Mgr.)
     92            '/\b(JUDr|Ph\.D|LL\.B|MUDr|Mgr|Bc|Ing|CSc|Th\.D|MBA|DiS|prof|doc|RNDr|PhDr|PaedDr|ThLic|Dr|BcA|MgA|PharmDr|MVDr|JUDr|ThDr|Ph\.Mr|prof|etc)\.\s+/u' => '$1.&nbsp;',
     93            '/(\d+\.)\s+(\S)/u' => '$1&nbsp;$2',
     94
     95            // Úhlové stupně, minuty, vteřiny
     96            '/(\d+)(\s*)(°|\'|\")/u' => '$1&nbsp;$3',
     97
     98            // Lomítka
     99            '/\s+\/\s+/u' => ' / ',
     100            '/([a-zA-Z])\/([a-zA-Z])/u' => '$1/$2',
     101
     102            // Exponenty a indexy
     103            '/(\d+)([²³])/u' => '$1$2',
     104
     105            // Tři tečky
     106            '/(\S)\s*\.{3}/u' => '$1&hellip;',
     107            '/\.{3}\s+/u' => '&hellip;&nbsp;',
     108            '/\s+\.{3}\s+/u' => '&nbsp;&hellip;&nbsp;',
     109            '/(\S)…(\s|&nbsp;)/u' => '$1&hellip;&nbsp;',
     110            '/\s+…\s+/u' => '&nbsp;&hellip;&nbsp;',
     111            '/…(\s|&nbsp;)/u' => '&hellip;&nbsp;',
     112
     113            // §
     114            '/\s+§\s+/u' => '&nbsp;§&nbsp;',
    56115        ];
    57116    }
Note: See TracChangeset for help on using the changeset viewer.