Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by LanX (Saint)
on Jul 22, 2022 at 17:15 UTC ( [id://11145661]=monkdiscuss: print w/replies, xml ) Need Help??

Hi

I just wanted to hack a free nodelet hack (sic) on the fly, which automatically links any used or required module in code-sections to CPAN.

But I don't know what I'm doing wrong, can't make it work yet.

Not sure if it's a JS problem or related to Chrome and security issues (couldn't test in FF yet).

Before scratching all the efforts, I thought it might be reasonable to ask for help.

paste this into the JS Console

function linkCPAN() { let codes = document.getElementsByClassName('codetext'); // let pattern = '$1<href="http://search.cpan.org/perldoc?$3">$3</ +a>'; let replacer = function (match, p1, p2, p3) { return p1 + '<href="http://search.cpan.org/perldoc?' + p3 + '" +>' + p3 + '</a>'; }; 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; } }

and call linkCPAN()

you should see the properly replaced HTML in the console, but nothing happens in the window.

here some input for testing

use strict; use Data::Dump;

Any ideas?

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

update

fixed JS has no hashes but objects bug with if (!codes.hasOwnProperty(i)) continue;

Replies are listed 'Best First'.
Re: Nodelet Hack to auto-linkify modules in code sections (works)
by LanX (Saint) on Jul 22, 2022 at 18:31 UTC
    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: monkdiscuss [id://11145661]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found