开发者

Creating an inline 'Jargon' helper in PHP

I have an article formatted in HTML. It contains a whole lot of jargon words that perhaps some people wouldn't understand.

I also have a glossary of terms (MySQL Table) with definitions which would be helpful to there people.

I want to go through the HTML of my article an开发者_JS百科d find instances of these glossary terms and replace them with some nice JavaScript which will show a 'tooltip' with a definition for the term.

I've done this nearly, but i'm still having some problems:

  • terms are being found within words (ie: APS is in Perhaps)
  • I have to make sure that it doesn't do this to alt, title, linked text, etc. So only text that doesn't have any formatting applied. BUT it needs to work in tables and paragraphs.

Here is the code I have:

$query_glossary = "SELECT word FROM glossary_terms WHERE status = 1 ORDER BY LENGTH(word) DESC";
$result_glossary = mysql_query_run($query_glossary);

//reset mysql via seek so we don't have to do the query again
mysql_data_seek($result_glossary,0); 

while($glossary = mysql_fetch_array($result_glossary)) {

    //once done we can replace the words with a nice tip
    $glossary_word = $glossary['word'];
    $glossary_word = preg_quote($glossary_word,'/');        
    $article['content'] = preg_replace_callback('/[\s]('.$glossary_word.')[\s](.*?>)/i','article_checkOpenTag',$article['content'],10);     
}

And here is the PHP function:

function article_checkOpenTag($matches) {
  if (strpos($matches[0], '<') === false) {
    return $matches[0];
  } 
    else {
        $query_term = "SELECT word,glossary_term_id,info FROM glossary_terms WHERE word = '".escape($matches[1])."'";
        $result_term = mysql_query_run($query_term);
        $term = mysql_fetch_array($result_term);

        # CREATING A RELEVENT LINK
        $glossary_id = $term['glossary_term_id'];
        $glossary_link = SITEURL.'/glossary/term/'.string_to_url($term['word']).'-'.$term['glossary_term_id'];

        # SOME DESCRIPTION STUFF FOR THE TOOLTIP
        if(strlen($term['info'])>400) {
            $glossary_info = substr(strip_tags($term['info']),0,350).' ...<br /> <a href="'.$glossary_link.'">Read More</a>';
        }
        else {
            $glossary_info = $term['info'];
        }


        return ' <a href="javascript:;" onmouseout="UnTip();" class="article_jargon_highligher" onmouseover="'.tooltip_javascript('<a href="'.$glossary_link.'">'.$term['word'].'</a>',$glossary_info,400,1,0,1).'">'.$matches[1].'</a> '.$matches[2];
  }
}


Move the load from server to client. Assuming that your "dictionary of slang" changes not frequently and that you want to "add nice tooltips" to words across a lot of articles, you can export it into a .js file and add a corresponding <script> entry into your pages - just a static file easily cacheable by a web-browser.

Then write a client-side js-script that will try to find a dom-node where "a content with slang" is put, then parse out the occurences of the words from your dictionary and wrap them with some html to show tooltips. Everything with js, everything client-side.

If the method is not suitable and you're going to do the job within your php backend, at least consider some caching of processed content.

I also see that you insert a description text for every "jargon word" found within content. What if a word is very frequent across an article? You get overhead. Make that descriptions separate, put them into JS as an object. The task is to find words which have a description and just mark them using some short tag, for instance <em>. Your js-script should find that em`s, pick a description from the object (associative array with descriptions for words) and construct a tooltip dynamically on "mouse over" event.


Interestingly enough, I was searching exactly NOT for a question like yours, but while reading I realized that your question is one that I had been through quite some time ago

It was basically a system to parse a dictionary and spits augmented HTML.

My suggestion would include instead:

  1. Use database if you want, but a cached generated CSV file could be faster to use as dictionary
  2. Use a hook in your rendering system to parse the actual content within this dictionary
  3. caching of the page could be useful too

I elaborated a solution on my blog (in French, sorry for that). But it outlines basically something that you can actually use to do that.

I called it "ContentAbbrGenerator" as a MODx plugin. But the raw of the plugin can be applied outside of the established structure.

Anyway you can download the zip file and get the RegExes and find a way around it.

My objective

  1. Use one file that is read to get the kind of html decoration.
  2. Generate html from within author entered content that doesnt know about accessibility and tags (dfn and or abbr)
  3. Make it re-usable.
  4. Make it i18n-izable. That is, in french, we use the english definition but the adaptative technology reads the english word in french and sounds weird. So we had to use the lang="" attribute to make it clear.

What I did

Is basically that the text you give, gets more semantic.

Imagine the following dictionary:

en;abbr;HTML;Hyper Text Markup Language;es
en;abbr;abbr;Abbreviation

Then, the content entered by the CMS could spit a text like this:

<p>Have you ever wanted to do not hassle with HTML abbr tags but was too lazy to hand-code them all!? That is my solution :)</p>

That gets translated into:

    <p>Have you ever wanted to do not hassle with <abbr title="Hyper Text Markup Language" lang="es">HTML</abbr> <abbr title="Abbreviation">abbr</abbr> tags but was too lazy to hand-code them all!? That is my solution :)</p>

All depends from one CSV file that you can generate from your database.

The conventions I used

  1. The file /abbreviations.txt is publicly available on the server (that could be generated) is a dictionary, one definition per accronym
  2. An implementation has only to read the file and apply it BEFORE sending it to the client

The tooltips

I strongly recommend you use the tooltip tool that even Twitter Bootstrap implements. It basically reads the title of any marked up tags you want.

Have a look there: Bootstrap from Twitter with Toolip helper.

PS: I'm very sold to the use of the patterns Twitter put forward with this Bootstrap project, it's worth a look!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜