ryan

Hanna Code

Recommended Posts

Easily insert any complex HTML, Javascript or PHP output in your ProcessWire content by creating your own Hanna code tags.
This module is based loosely on the WordPress Hana Code Insert plugin.
 
post-2-0-11674600-1370199246_thumb.png
 
post-2-0-50116800-1370199239_thumb.png
 
A Hanna code tag looks like [[hello_world]]. A Hanna code tag with attributes looks like [[hello_world foo=bar" bar="foo]] using HTML style attributes or [[hello_world foo=bar, bar=foo]] using ProcessWire selector style attributes. After installing the module, you define your Hanna codes in Setup > Hanna Code.
 
These Hanna codes that you define can then be entered within your body copy (or other text where you allow) and they will be replaced with the values defined or generated by your Hanna code. A common use case is to embed scripts or other bits of HTML or codes that would usually be stripped out by an editor like TinyMCE. However, Hanna codes can be more than just static snippets--they can be dynamic PHP or Javascript that outputs different things according to the request. PHP-based Hanna codes have access to the entire ProcessWire API.
 
Hanna code accepts named attributes in the tag that can be passed as variables to PHP and Javascript Hanna codes. These attributes can be specified either in HTML attribute format or ProcessWire selector format. In either case, quotes should be used around the attribute value when the value contains whitespace or a comma.
 
How to install
  • Place the module files in /site/modules/TextformatterHannaCode/
  • In your admin, click Modules > Check for new modules
  • Click install for TextformatterHannaCode
  • Now to go Setup > Fields and locate the Textarea field(s) that you want to use Hanna codes with ("body" for instance). 
  • When editing the field, click the details tab, and select "Hanna Code" as the Textformatter. Save.
  • Now go to Setup > Hanna Code and start defining your Hanna Codes! You may want to use one of the examples from this document to get started.
Tag format
 
Below is a Hanna code tag named hello_world with no attributes. If you pasted this into your body copy, you would get whatever the replacement value is that you defined.
[[hello_world]]
Below is a Hanna code tag named hello_world being passed attributes of foo, bar and foobar. If this were a PHP-based Hanna code, it would receive the variables $foo, $bar and $foobar:
[[hello_world foo="bar" bar="foo" foobar="foo bar"]]
Below is the same Hanna code tag as above, but with attributes more like ProcessWire selectors. You can use whatever format you prefer. Just note that unlike regular ProcessWire selectors, quotes (single or double) are required around any value that has whitespace.
[[hello_world, foo=bar, bar=foo, foobar="foo bar"]] 
How to use
 
Please make sure that you have completed the How to install section first. Then in your admin, go to Setup > Hanna Codes.
 
Each Hanna code that you add has a type of either: Text/HTML, Javascript or PHP. The Text/HTML type is literally self explanatory in that your [[custom-tag]] is replaced with exactly the text you paste in. Anywhere that you type your [[custom-tag]] in your body copy will be replaced with exactly the static text you defined.
 
More power opens up with the Javascript and/or PHP types of codes. These codes execute at runtime and thus can contain specific logic to produce different results. In fact, PHP Hanna codes have access to the entire ProcessWire API and are executed in the same manner as template files. Your PHP-based Hanna code should simply "echo" or "print" the replacement value.
 
PHP example
  • Create a new Hanna code with the name "children".
  • Select "PHP" as the type.
  • Paste in the following for the code:
foreach($page->children as $child) {
  echo "<p><a href='$child->url'>$child->title</a>";
}
  • Now go and edit a page that has children. In the body copy, enter [[children]] in the place where you want the output to appear.
  • View the page, and you should see the rendered list of links to children.
PHP example, part 2
 
Now lets take the above example further...
  • Go back and edit your "children" Hanna code, as we are going to modify it to respond to a "parent" attribute. Change the code to this:
if(isset($parent)) {
  // If $parent is an ID or path, lets convert it to a Page
  $parent = $pages->get($parent);
} else {
  // otherwise lets assume the current page is the parent
  $parent = $page; 
}

foreach($parent->children as $child) {
  echo "<p><a href='$child->url'>$child->title</a>";
}
  • Go back and edit the page where you previously inserted the [[children]] tag, and change it to: [[children, parent=1]] (specifying the homepage) or [[children, parent=/path/to/some/parent/]] if you want to try something else.
  • View the page and you should now see it showing the children of the homepage (or of another parent you specified).
Please see the Javascript and PHP usage notes on the Hanna code entry screen.
 
Security
 
There are major security implications with a tool that will let you enter unfiltered text and code from your web browser. As a result, Hanna codes are meant for definition only by superusers and we recommend keeping it that way.
 
Download
 
Download the Hanna Code module from the ProcessWire modules page or from GitHub
  • Like 33

Share this post


Link to post
Share on other sites

Just a note to all MODx'ers - I think this is a nice way to transfer some snippets/chunks to ProcessWire as well if people want to go down that route and have some they used to use in body text. Obviously it's not quite the same thing (potentially more powerful depending on your needs), but anything that helps make a transition easier is good in my book :)

Very useful, powerful module ryan!

  • Like 3

Share this post


Link to post
Share on other sites

Wow! Great module, Ryan.

Gonna create a bunch of snippets for quick prototyping and trying things. Expect a new wave of MODx'ers on the forum. These folks love using code snippents the way Hanna Code allows it!

Edit: Pete, we had almost the same thought!

  • Like 1

Share this post


Link to post
Share on other sites

Wow! Nice one; you've been busy!

Yes, there have been two or three questions from MODx'ers in the forums about something like this - the ability to randomly, directly call snippets, chunks and TVs within the body field/area in TinyMCE. Previously they've been pointed to shortcodes and tag parser. Hanna is certainly more powerful and would solve this issue easily.

I can't wait to see the creative ways people use this :rolleyes: .

Thx for the module!

  • Like 2

Share this post


Link to post
Share on other sites

Looks like a very useful module, thanks for sharing it Ryan.

I just tried to install it using ModulesManager, and I can't install it right after downloading as it requires Textformatter module to be installed.

TemplateFile: Module ProcessHannaCode requires: TextformatterHannaCode

#0 [internal function]: Modules->___install('ProcessHannaCod...')...

Couldn't it be installed when installing the process module?

Edit: When I create a new hanna code and not enter a tag name and save I get an error and can't see the form.

  • Like 1

Share this post


Link to post
Share on other sites

This looks brilliant Ryan, thank you! There are parallels with a nice aspect of Textpattern here also, TXP 'Forms' (snippets). Brill! ^_^

  • Like 1

Share this post


Link to post
Share on other sites

Looks like a very useful module, thanks for sharing it Ryan.

I just tried to install it using ModulesManager, and I can't install it right after downloading as it requires Textformatter module to be installed.

TemplateFile: Module ProcessHannaCode requires: TextformatterHannaCode

#0 [internal function]: Modules->___install('ProcessHannaCod...')...

Couldn't it be installed when installing the process module?

Edit: When I create a new hanna code and not enter a tag name and save I get an error and can't see the form.

Works fine if you use default Module manager (PW's). You can only install via TextformatterHannaCode actually. Summary says this "Also Installs - ProcessHannaCode". The install button for  ProcessHannaCode is greyed out with the message "Requires - TextformatterHannaCode". ProcessHannaCode auto installs. Maybe something with ModulesManager?

@Ryan: The TextformatterHannaCode and ProcessHannaCode have the same name - "Hanna Code" in PW module manager. This can be confusing :) (but not a big deal).

post-894-0-28482800-1370266185_thumb.png

Share this post


Link to post
Share on other sites

@kongodo, but I don't use Modules page anymore :D I didn't see that the Textformatter is the mother module. At the end it got installed but it's confusing.

Of course it has to do with ModulesManager and the nature of the modules. Modules Manager doesn't know what modules are there and how to install. It takes the module "class name" that is entered on modules.processwire.com to create folder and installs the main module. It's all information I got from modules.processwire.com and there's no manifest, dependencies or lifecycle informations to modules in PW (which IMO is requires rather sooner than later)

For some reason it seems now Ryan has changed the module class name to "TextformatterHannaCode", and this should work better now. Though changing those settings "class name" is dangerous once the module is installed via ModulesManager, because there's actually two different folders for the same module.

Share this post


Link to post
Share on other sites
I just tried to install it using ModulesManager, and I can't install it right after downloading as it requires Textformatter module to be installed.

Sorry about that, I went back and forth on the name a couple times and ultimately entered the wrong class name in the modules directory. It should have been TextformatterHannaCode, not ProcessHannaCode. I have corrected that.

Edit: When I create a new hanna code and not enter a tag name and save I get an error and can't see the form.

I was throwing an exception when I shouldn't have. I'm pushing a correction for this. 

 The TextformatterHannaCode and ProcessHannaCode have the same name - "Hanna Code" in PW module manager. This can be confusing :) (but not a big deal).

Technically they are part of the same program "Hanna Code", even if that program has two modules. But I know what you mean. I'm adding "(Process)" to the Process one, just so that the two next to each other can be differentiated. 

Also, one more section I just added to the README:

Using Hanna Code from the API
 
If you want to populate Hanna Code from the API, you can do so using the render() method, like this:
$hanna = $modules->get('TextformatterHannaCode'); 
$page->body = $hanna->render($page->body); 
  • Like 8

Share this post


Link to post
Share on other sites

Just posted version 0.0.2 that addresses the items above. It also adds an option that lets you specify whether the immediate surrounding HTML tag should be replaced with your Hanna Code output or not. For instance, if your [[hello-world]] Hanna Code output is "Hello World" and is applied to the body copy containing "<p>[[hello-world]]</p>" then you probably don't want it to replace the surrounding <p> tags. Whereas if your Hanna Code outputs block level elements like "<ul><li>Hello World</li></ul>" then you probably do want it to replace the surrounding <p> tags, otherwise you'd end up with some invalid markup like: <p><ul><li>Hello World</li></ul></p>.

  • Like 2

Share this post


Link to post
Share on other sites

I have tested Hanna with single {, ( and [, i.e. 

(hello)
[hello]
{hello} 
They all work fine in both PHP and Text mode.

Ryan, are these OK or they could throw up issues? Thx.

Share this post


Link to post
Share on other sites

I think it would be really cool if there was and easy (JSON?) way to import/export hanna codes (all or single). Maybe then we could setup a subforum to share them.

Otherwise this is amazing, thanks Ryan.

Share this post


Link to post
Share on other sites

I think it would be really cool if there was and easy (JSON?) way to import/export hanna codes (all or single). Maybe then we could setup a subforum to share them.

Otherwise this is amazing, thanks Ryan.

/site/assets/cache/HannaCode/ :). The PHP are stored as PHP files

Share this post


Link to post
Share on other sites

Is it just me or is Hanna version 1.0.4 broken? It no longer works - it outputs the raw tag :(

Don't know what's happening but even older versions do not work any more. I'm stumped. Ideas? Using it on 2.3 stable

Edited by kongondo

Share this post


Link to post
Share on other sites

/site/assets/cache/HannaCode/ :). The PHP are stored as PHP files

Haha, thats cool, I guess I could've taken a look before posting. Though a spot to officially discuss these maybe couldn't hurt. :P

Share this post


Link to post
Share on other sites

Is it just me or is Hanna version 1.0.4 broken? It no longer works - it outputs the raw tag  :(

Don't know what's happening but even older versions do not work any more. I'm stumped. Ideas? Using it on 2.3 stable

It's working for me at least, though I did change some things about the regex's in 0.0.4, so I might need to get more info about the context. What's an example of a tag that isn't working for you? What is the markup that surrounds it in your text? Also can you double check that "Hanna Code" is set as the textformatter for your body field (or whatever field you are using it in) in Setup > Fields > body > details?

Ryan, are these OK or they could throw up issues? Thx.

It should be fine to set your open/close tags to be whatever you want. Though [this] is a fairly common text construct, where as [[this]] is not. So if your [this] ever conflicts with the name of a Hanna Code, that could cause some confusion. As a result, I think it may be preferable to use open/close tags that don't duplicate things you would usually see in written copy. 

Share this post


Link to post
Share on other sites

It's working for me at least, though I did change some things about the regex's in 0.0.4, so I might need to get more info about the context. What's an example of a tag that isn't working for you? What is the markup that surrounds it in your text? Also can you double check that "Hanna Code" is set as the textformatter for your body field (or whatever field you are using it in) in Setup > Fields > body > details?

Sorry; I just wasted your precious time! I forgot to set it as my textformatter after I made a couple of changes!  :-[ Thanks :)

Share this post


Link to post
Share on other sites

What kills me about this module is that Ryan literally decided to make it because I had something similar in WordPress and needed a substitute solution in ProcessWire (as part of the move of CMS Critic to PW) otherwise I'd have a ton of weird shortcodes in my posts for no reason. He made this module, imported my code from the other module (called Hana Code in WP) and named his Hanna Code (after his daughter) and Poof! a module is born.

I am insanely jealous of his mad php skills (but glad they can be bought!)

  • Like 10

Share this post


Link to post
Share on other sites

Ryan, brilliant stuff!
 

For me it won't work in my first attempt and it took me some time to figure out my failure.

You can't use the identifier "name" for a variable.

[[gallery name="Test"]]

I didn't found any comments on this issue, so perhaps it helps.

  • Like 2

Share this post


Link to post
Share on other sites

Kent, that's correct–I'll update the readme file. The term "name" is set to the name of the Hanna code. This is provided to the PHP so that it can check the name of the code, just in case it needs it. Likewise, it's preferable not to use any API variable name for your attributes either. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By flydev
      PulsewayPush
      Send "push" from ProcessWire to Pulseway.
       
      Description
      PulsewayPush simply send a push to a Pulseway instance. If you are using this module, you probably installed Pulseway on your mobile device: you will receive notification on your mobile.
      To get more information about Pulseway, please visit their website.
      Note
      They have a free plan which include 10 notifications (push) each day.
       
      Usage
      Install the PulsewayPush module.
      Then call the module where you like in your module/template code :
      <?php $modules->get("PulsewayPush")->push("The title", "The notification message.", "elevated"); ?>  
      Hookable function
      ___push() ___notify() (the two function do the same thing)
       
      Download
      Github:  https://github.com/flydev-fr/PulsewayPush Modules Directory: https://modules.processwire.com/modules/pulseway-push/  
      Examples of use case
      I needed for our work a system which send notification to mobile device in case of a client request immediate support. Pulseway was choosen because it is already used to monitor our infrastructure.
      An idea, you could use the free plan to monitor your blog or website regarding the number of failed logins attempts (hooking Login/Register?), the automated tool then block the attacker's IP with firewall rules and send you a notification.
       

       
      - - - - - - - - - - - - - -
      11-22-2017: added the module to the modules directory
       
    • By netcarver
      Part 1 of a 2 part Module & Service Reveal.
      I'm currently working on a new module: ModuleReleaseNotes that was inspired by the work I originally did on making Ryan's ProcessWireUpgrades module "release" aware. In the end, I decided to ditch the approach I was originally taking and instead work on a module that hooked in to the UpgradeConfirmation dialog and the module edit page.
      Aims
      My aims for this module are as follows...
      Make discovery of a module's changes prior to an upgrade a trivial task. Make breaking changes very obvious. Make reading of a module's support documentation post-install a trivial task. Make module authors start to think about how they can improve the change discovery process for their modules. Make sure the display of information from the module support files/commit messages doesn't introduce a vulnerability. Looking at these in turn...
      Making discovery of a module's changes prior to upgrade a trivial task.
      This is done by adding a "What's changed section" to the upgrade confirmation dialog.  This section takes a best-effort approach to showing what's changed between the installed version and the updated version that's available via the module repository.
      At present, it is only able to talk to github-hosted repositories in order to ask them for the release notes, the changelog file (if present) and a list of commits between the git tag that matches the installed version and the tag matching the latest version.
      It will display the Release Notes (if the author is using the feature), else it will display the commits between the tags (if tagging is used by the module author) else it will show the changelog file (if present) else it will show the latest N commits on the master branch (N, of course, being configurable to your liking.)
      An example of the Github Release Notes pulled in for you, taken from Mike Rockett's TextformatterTypographer Module...

      An example of a tag-to-tag commit list from the same module...

      An example of a changelog - formatted to show just the changes (formatting styles will change)...

      Finally, an example of a fallback list of commits - sorry Adrian ...

       
      Making breaking changes obvious.
      This is currently done by searching for a set of configurable search strings. Later versions may be able to support breaking change detection via use of Semantic Versioning - but this may require some way of signalling the use of this versioning standard on a module-by-module basis.
      For now, then, you can customise the default set of change markers. Here I have added my own alias to the list of breaking change markers and the changes section of the changelog is styled accordingly (these will be improved)...

       
      Make reading of a module's support documentation, post-install, a trivial task.
      This is done by making some of the support files (like the README, CHANGELOG and LICENSE files) readable from the module's information/settings screen. There is an option to control the initial open/closed state of this section...

      Here is Tracy's README file from within the module settings page... 

       
      Make module authors start to think about how they can improve the change discovery process for their modules.
      There are notes in each of the sections displayed on the upgrade confirmation page that help authors use each of the features...

       
      Make sure display of external information doesn't introduce a vulnerability.
      This is an ongoing concern, and is the thing that is most likely to delay or prevent this module's release lead to this module's withdrawl should a vulnerability be found. Currently, output is formatted either via Markdown + HTML Purifier (if it was originally a Markdown file) or via htmlspecialchars() if it has come from a plaintext file.
      If you discover a vulnerability, please get in contact with me via the forum PM system.
       
      Ongoing...
      For now, I've concentrated on integration with GitHub, as most people use that platform to host their code. I know a few people are hosting their repositories with BitBucket (PWFoo comes to mind) and some with GitLab (Mike Rockett?) and I would eventually like to have adaptor implementations for these providers (and perhaps GitKraken) - but for now, GitHub rules and the other hosts are unsupported.
       
      Links
      Github: ModuleReleaseNotes
      PW Module Repository: Here
    • By FrancisChung
      Hi,
      I was wondering if anyone knew what this did? 

      If so, do you know what modules it actually impacts? Is there another log that lists the action it took?

      I'm having some weird problems with PHP duplicate declaration per link below, since this module was run.
       
       
    • By tpr
      AdminOnSteroids
      Various admin tweaks to enhance ProcessWire admin.
      http://modules.processwire.com/modules/admin-on-steroids/
      https://github.com/rolandtoth/AdminOnSteroids