Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Smartypants Laravel Package

michelf/php-smartypants

PHP port of John Gruber’s SmartyPants: converts plain ASCII punctuation into typographically correct “smart” quotes, dashes, and ellipses. Useful for cleaning up HTML output from Markdown or other text sources, with configurable transformation options.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install: Require via Composer: composer require michelf/php-smartypants.
  2. First use: Instantiate the parser and process Markdown or plain text—e.g., echo \Michelf\SmartyPants::defaultTransform('He said -- "Hello world."'); → outputs proper en-dash and curly quotes.
  3. Key entry point: \Michelf\SmartyPants class; default static method defaultTransform() covers most common typographic conversions (quotes, dashes, ellipses).
  4. Check docs: While minimal, the repo README outlines syntax variations and options.

Implementation Patterns

  • Post-processing Markdown: Use in your pipeline after Markdown parsing (e.g., with league/commonmark), as SmartyPants enhances rendered HTML—not raw Markdown.
  • Blade integration: Create a custom Blade directive:
    Blade::directive('typo', fn ($expression) => "<?php echo \\Michelf\\SmartyPants::defaultTransform({$expression}); ?>");
    
    Usage: @typo('She said -- "Let\'s go!"').
  • Middleware approach: Wrap response content with SmartyPants::defaultTransform() in a global response middleware for site-wide typographic polish.
  • Configurable variants: Use SmartyPants::process($text, $attr) to enable options like 2 (smart quotes only), 3 (no quotes), or custom settings like ['smart_quotes' => false].
  • Inline for CMS: In admin edit views, preview rendered output via this package to avoid "ugly" punctuation during content editing.

Gotchas and Tips

  • HTML safety: The class does not parse HTML—it assumes input is plain text or pre-sanitized. Passing raw HTML can corrupt tags (e.g., <a href="x">--y</a><a href="x">—y</a>), so strip tags first if needed: SmartyPants::defaultTransform(strip_tags($text)).
  • No Markdown support: It’s not a Markdown parser—only processes text/sanitized HTML. Do not apply to raw Markdown source (e.g., # Title -- My Note will mangle -- inside headings).
  • Encoding matters: Ensure input is UTF-8; non-ASCII dash/quote chars may not convert as expected if encoding is mismatched.
  • Performance: Light, but avoid calling on large documents repeatedly—cache final output or use middleware-level caching.
  • Extensibility: Though not PSR-12–clean, subclass \Michelf\SmartyPants to override applyRules() for custom rules (e.g., adding Spanish guillemets).
  • No auto-detection: Dashes like -- become en-dashes always, --- em-dashes—no context-awareness. For technical docs, disable dashes via options if fidelity to code snippets is needed.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport