Customizing SMF > SMF Coding Discussion

Automatically link certain words?

<< < (2/8) > >>

MrMike:
I have a working example of the word-linker code that I want to clean up and (possibly) improve a bit, but I should have something within the next day if anyone is interested.

My thought is to use it as an include file, with the include line inserted just before the post body is output. In SMF 2.02, in the Themes/default/Display.template.php file, this would be right around line 507, just before this bit of code:


--- Code: ---echo

    $message['body'], '</div>
--- End code ---

(Someone who knows something about making mod packages could probably suggest an alternative method if this way of doing it isn't the best way.)

In a nutshell, the var $message['body'] gets intercepted, run through the word linker, and the changed text is put back into the $message['body'] var, where it's then displayed.

Right now the word-linker links only the first occurrence of any given word, but this could be configured on a per-word basis through an option setting. That is, you could make it link the word 'dog' only once, link 3 instances of the word 'cat' , link 1 instance of the word 'house', and so on.

It also avoids link words that are inside of an existing link, so you won't get double-linked words or mangled links. :)

:
I wouldn't put it in the display template if at all possible. Aside from the fact you're doing processing and not pure display (which means it's semantically, if not technically, the wrong place), what you end up doing is applying the changes to all themes.

Instead, attach the code into Display.php, inside prepareDisplayContext() and adjust $output['body'] there before it's returned back to Display.template.php as $message.

MrMike:

--- Quote from: Arantor on May 28, 2012, 08:47:22 AM ---Aside from the fact you're doing processing and not pure display (which means it's semantically, if not technically, the wrong place), what you end up doing is applying the changes to all themes.
--- End quote ---

You're correct- it's both technically and semantically the wrong place. :)


--- Quote from: Arantor on May 28, 2012, 08:47:22 AM ---Instead, attach the code into Display.php, inside prepareDisplayContext() and adjust $output['body'] there before it's returned back to Display.template.php as $message.
--- End quote ---

Yes, this is the proper spot to do the transform...thank you for this, Arantor. It looks like the best spot in Display.php is immediately after the line where the BBC interpreter is run.

Currently I've got it working so you can specify the number of times each word is linked, plus you can include an optional style or class tag so the altered links can be made to look a little different (if you want).

You can see a demo of it working on these pages:

Test Page:
http://deltabravo.net/forum/index.php?topic=39855.msg318677#msg318677

(and here  and here as well.)

I can post the code and instructions for the modifications required (minor), but I'd really like to work with someone who can turn this into a mod package.

There are only a few words linked (don't want to overdo it), but I'll be adding entries for the 50 or so most commonly used terms worth linking.

MrPhil:
That looks pretty nice. You mentioned doing it after BBCode processing -- have you confirmed that you generate links only once per page (not once per post)? Are all links of the same format (in your case, fed to search), or could you have some words that generate, say, links to external sites, and some that just get a &trade; or &reg; mark added? Since each word in the list gets its corresponding output listed, there's probably no need to get fancy with variable substitutions and such:

'Brillo'  'after'  ''  '&trade;'

would just be entered as

'Brillo'  'Brillo&trade;'

unless you wanted to match both upper and lower case forms and use the original in the output:

'Brillo'  'Brillo&trade;'
'brillo'  'brillo&trade;'

What if someone miscapitalizes, or obFusCates in order to hide their usage? In that case, you might want to do something like

'/(Brillo)/i'   '$1&trade;'

using preg_match(). Something to think about.

Does this thing work only on BBCode-processed text? Should it be done on topic subject lines, or in the breadcrumb trail or page title? Do signatures get normal BBCode processing?

As a labor saving device, maybe for similar but not identical outputs:

'term1,term2,term3,term4'  'function definition to process'   '<a href="/$1/$2.php">$3</a>'

where $1 and $2 might be generated from the matched term, and $3 is the original term.

MrMike:

--- Quote from: MrPhil on May 28, 2012, 03:45:59 PM ---That looks pretty nice. You mentioned doing it after BBCode processing -- have you confirmed that you generate links only once per page (not once per post)?
--- End quote ---

Right now it's running per post; I'm working on making it generate words once per page. Trying to save a few queries if I can.



--- Quote from: MrPhil on May 28, 2012, 03:45:59 PM ---Are all links of the same format (in your case, fed to search), or could you have some words that generate, say, links to external sites,
--- End quote ---

Right now they just go to various hand-selected searches or in some cases a specific page. The links can go anywhere you want, on a per word basis.



--- Quote from: MrPhil on May 28, 2012, 03:45:59 PM ---What if someone miscapitalizes, or obFusCates in order to hide their usage?
--- End quote ---

It's doing a case insensitive search so I'm not too concerned about people obfuscating their words. For the target phrase 'court order' it'll catch 'court order', 'Court order', Court Order', 'COURT Order", and so on, and it'll replace the phrase with the same case as whatever it was before it got linked.



--- Quote from: MrPhil on May 28, 2012, 03:45:59 PM ---Does this thing work only on BBCode-processed text? Should it be done on topic subject lines, or in the breadcrumb trail or page title? Do signatures get normal BBCode processing?
--- End quote ---

I think it'll work on any text or HTML as far as I know.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version