Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Nodelet Hack to auto-linkify modules in code sections (works)

by LanX (Saint)
on Jul 22, 2022 at 18:31 UTC ( [id://11145662]=note: print w/replies, xml ) Need Help??


in reply to Nodelet Hack to auto-linkify modules in code sections (well almost)

OK, I think I got trapped into some JS's regex voodoo or new security settings.

Anyway this seems to work in Chrome and FF , copy it into your Free Nodelet Settings and after clicking the link all modules after use or require are underlined and have an onclick event to call CPAN in a _blank window.

This is beta-code, no guaranties whatsover! =)

<!-- ================================================================= +===================== Auto-Linkify Modules in Code Sections Version 0.31.03 2022-07-23 https://perlmonks.org/?node_id=11145662 --> <script> // <!-- function show_module(module) { let url = 'https://perldoc.perl.org/' + module; window.open(url,'_blank'); } function link_modules() { let codes = document.getElementsByClassName('codetext'); let replacer = function (match, p1, p2, p3) { return p1 + '<u><span onclick="show_module(\'' + p3 +' \')">' ++ p3 + '</span></u>'; }; for (var i in codes) { if (!codes.hasOwnProperty(i)) continue; let text = codes[i]; let inner = text.innerHTML; inner = inner.replace(/((use|require)\s+)((\w+)(::\w+)*)/g, re +placer); // console.log(inner); text.innerHTML = inner; } } /* Automatic linking */ link_modules(); // comment out if you prefer button only // --> </script> <!-- Explict button in the Free Nodelet ... remove if automatic linkin +g is enough --> <button onclick='link_modules()'>link_modules() [id://11145662|v0.31.3 +]</button> <!-- END Auto-Linkify Modules in Code Sections ----------------------- +------------------ -->

to do

  • better identifier parsing then just \w+
  • delay link_CPAN to window.onload or similar for automatic linking
  • make target window configurable
  • hide functions in own namespace / object
  • make it easily importable like the wikisyntax hack
  • detect core modules and link to perldoc instead(?) °,²
  • exclude embedded package names (?)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

changes
  • updated to v0.3, transformation happens now automatically
  • ²) 2022-07-23 v0.31 https://perldoc.perl.org/MODUL is already redirecting to MetaCPAN so no need to check corelist
updates
  • °) here the output from corelist for v5.32.0

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145662]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found