Provide option to load jsMath only on pages that contain math code

oash - July 25, 2008 - 16:32
Project:JsMath for displaying mathematics with TeX
Version:6.x-1.1
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Hi,

I was wondering how to prevent jsMath from loading in certain pages, especially all admin pages admin/*, these pages contains some '$' and hence jsmath is processing them and output undesired text, especially in the view setting pages.

Thanks

#1

gustav - July 26, 2008 - 07:24

This is something I had planned to do but did not get around to yet. The following is in the future plans:

"Currently this module does not provide filters which can be added to input
formats but instead the jsMath processing applies to all Drupal pages once
the module is enabled. In a future release this will be changed, using the
mechanism for selective processing of parts of a page described by Davide in
the forum post at https://sourceforge.net/forum/message.php?msg_id=4413480

#2

AlexisWilke - November 18, 2008 - 07:15
Version:5.x-1.1» 6.x-1.1

I made jsMath a filter for D6. I do not have D5...

There are two issues, to my point of view, you probably want to create specific Input Format entries so as to turn on the feature just and only on these few specific nodes. And the filter is the piece of code that adds the necessary JavaScript to start jsMath. This means we cannot make use of the cache, unfortunate side effect.

None the less, this makes my admin pages DEAD FAST, in comparison. Although I have another JS that seems to do a similar thing because I still have some slowness, at least now I cannot blame jsMath for it!

This patch also includes the code that adds a SPAN tag around the [math] and [/math] (or whatever tag name that's used.) The span is given the special class name "jsmath". One thing in a specific theme, we could HIDE these span by default, and show them only after the math formula was transformed. Just an idea. That would require a change of the style once the jsMath function ran against a block.

Note that I did not use a DIV since that would break jsMath formulae that are written inline within a paragraph.

I'm offering a tarball for easy install, but the patch is otherwise going against 6.x-1.1. It is not intended as a fork, but... I strongly suggest to create a version 2.x since this is a completely different approach that will "break" existing sites (i.e. existing sites will see their formulae disappear... until they turn on the jsMath filter or create a new Input Format with the jsMath filter activated.)

Sorry if I do not support 5.x. It's old 9-)

Okay, now I can really love it...

Thank you.
Alexis Wilke

AttachmentSize
jsmath-6.x-1.1-filter.patch 5.77 KB
jsmath-6.x-1.1-as-filter.tar_.gz 12.76 KB

#3

AlexisWilke - November 18, 2008 - 19:08

Actually, there is an even better way. I will post a new entry later for it.

The idea is simple, inside the <span ...> we can have an event (probably won't work) or at least a unique ID. Then we can generate a list of <script> entries that reference/list these IDs. Actually, just an array would work. Then you do not need to parse anything in Javascript, all is done in PHP in Drupal. And that can be done one and cached. So it would be a lot better.

Thank you.
Alexis

#4

gustav - November 18, 2008 - 19:23

Alexis,

thanks for your work on this.

Have you read through the jsMath website to see if there is not some advice on how to best do what you are trying to do? See http://www.math.union.edu/~dpvc/jsMath/authors/welcome.html

#5

carnage - July 28, 2009 - 17:02

I have a better solution. Which i'm part way through implementing.

Firstly, move the js math loading functionallity into a separate function (rewritten to make use of drupals js adding facilities)

Next, we use hook_nodeapi $op=alter to search the finial node text and teaser for $,$$ and any custom math delimiters. Js math is the ONLY loaded if at least one of the delimiters is found in the finial output text.

If another module wishers to make use of jsmath, they can use something like if(function_exists('loadjsmaths')) { loadjsmaths() } to load it on pages that require it.

#6

AlexisWilke - July 28, 2009 - 18:48

carnage,

Won't work with the default Drupal cache. You need the JS to be added in the hook_init() function. There is documentation about that on the drupal site if you feel like verifying what I'm saying.

Thank you.
Alexis Wilke

#7

carnage - July 29, 2009 - 10:44

I have to disagree.

With caching enabled, this loading technique appears to work for both logged in and anon users.

also: http://api.drupal.org/api/function/drupal_add_js makes no reference to the javascript needing to be added in hook_init. In fact the opposite is implied by the list of functions that call drupal_add_js.

It is true, that the javascript cache thing dosn't work for JSmaths using this technique, however due to the way js maths loads itself, the cache is mostly rendered useless for jsmaths anyway

I've attached the modified module file if you want to test it for yourself. (I'd have made a patch but the jsmath file had already been modified. So I didn't have a common base.)

AttachmentSize
jsmath.module.txt 18.53 KB

#8

AlexisWilke - July 29, 2009 - 17:59

Okay, I agree that if you make the JS inline then the caching will work since the code is part of the HTML file! 8-)

Thank you.
Alexis Wilke

#9

carnage - July 30, 2009 - 09:54
Status:active» needs review

My first post wasn't too clear on my intentions as I was still experimenting with different solutions.

I think the ideal solution would be to give the admin the option of having jsmaths load on every page or just on the pages that maths is detected on.

#10

AlexisWilke - July 30, 2009 - 16:20

Because it is so slow to load, loading on all pages can be a big waste unless 99% of your pages include math expressions. The detection is so fast that the gain is visible immediately! That's a problem for slower computers that have to run all that Javascript code on pages that don't even include expressions...

Also I would suggest that you generate a patch starting from 6.x-1.1 so that way it shows clearly what has changed. You just have to extract the tarball in another directory and then apply something like:

diff -dur jsmath-6.x-1.1 jsmath >jsmath.patch

#11

turadg - August 19, 2009 - 01:04
Title:How to prevent jsMath from loading in certain pages» Provide option to load jsMath only on pages that contain math code
Category:support request» feature request

The changes carnage described in #5 are quite useful and his code in #7 looks good.

gustav, what is your opinion on including these improvements?

 
 

Drupal is a registered trademark of Dries Buytaert.