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

Re: PerlMonks Editor

by jdporter (Chancellor)
on Apr 18, 2006 at 21:51 UTC ( #544215=note: print w/replies, xml ) Need Help??

in reply to PerlMonks Editor

That is nice, because it attempts to be PerlMonks markup aware. Here's the thing I use to edit nodes locally using any editor, whether vi, Netscape Composer, or whatever.

As you can see, the default editor I have hardcoded is my local installation of Mozilla Editor (formerly aka Netscape Composer, I think).
use LWP::UserAgent; use HTML::Entities qw( decode_entities ); use HTTP::Request::Common qw(POST); use Getopt::Long; use strict; use warnings; my $base_url = ""; my $edit_cmd = '"C:\\Program Files\\\\Mozilla\\mozilla.exe" + -editor "file://%s"'; my $node_id; my $username; my $password; GetOptions( 'node_id|id=s' => \$node_id, 'username=s' => \$username, 'password|pw=s' => \$password, 'editor|edit_cmd=s' => \$edit_cmd, ); $edit_cmd =~ /%s/ or $edit_cmd .= ' "%s"'; sub prompt_for { my $p = shift; print "\n$p: "; local $_ = <>; chomp; $_ =~ /^$/ and die "aborted.\n"; $_ } $node_id ||= prompt_for('NodeID'); $username ||= prompt_for('UserName'); $password ||= prompt_for('Password'); my %params = ( op => 'login', user => $username, ticker => 'yes', displaytype => 'xml', xmlstyle => 'flat', node_id => $node_id, ); my $ua = LWP::UserAgent->new; $ua->agent("NodeEditor/0.1 "); my $params = join '&', map { $_ . '=' . $params{$_} } keys %params; my $req = HTTP::Request->new( GET => $base_url.'?'.$params ); my $res = $ua->request($req); $res->is_success or die "GET Error: " . $res->status_line . "\n"; $_ = $res->content; my( $title ) = /<node .*\btitle="([^"]*)"/; my( $text ) = /<doctext\b[^>]*>(.*)<\/doctext>/s; $text = decode_entities( $text ); my $text_has_dos_eoln = $text =~ /\r\n/ && $text !~ /[^\r]\n/; $text =~ s/\r//g; my $filename = "node_$node_id.html"; my $perl_filename = "$ENV{TMP}/$filename"; open F, "> $perl_filename" or die "write $perl_filename - $!\n"; print F "<html><head><title>$title</title></head><body>\n$text\n</body +></html>\n"; close F; print "\n"; system sprintf $edit_cmd, $perl_filename; open F, "< $perl_filename" or die "read $perl_filename - $!\n"; $_ = do { local $/; <F> }; close F; s/^<!DOCTYPE[^>]*>\s*//i; my( $new_title, $new_text ) = /^<html><head><title>([^<]*)<\/title><\/head><body>(.*)<\/body><\/html +>/s or die "you screwed up the format!"; #die "title='$new_title'\n\n'$new_text'\n\n"; $text_has_dos_eoln and $new_text =~ s/\n/\r\n/g; $req = POST $base_url, [ %params, sexisgood => "update", note_title => $new_title, note_doctext => $new_text, passwd => $password, ]; $res = $ua->request($req); $res->is_success or die "POST Error: " . $res->status_line . "\n"; print $res->status_line, "\n";
We're building the house of the future together.

Replies are listed 'Best First'.
Re^2: PerlMonks Editor
by GrandFather (Saint) on Apr 18, 2006 at 22:08 UTC

    This looks pertinent to version 2 of the editor - scrape the node being replied to (to allow quoted material from the OP) and post the reply directly. Thanks for the reply!

    DWIM is Perl's answer to Gödel

      In your code the following line

      use constant kParaSpace => 4;

      appears to be the setting for the gap between paragraphs when the paragraph tags are used.

      When I tested your editor the gap between paragraphs is rather wider than seems to be the norm in most PerlMonks posts. Perhaps kParaSpace should be set to 2?

      This is a minor nitpick; otherwise this is very slick. Kudos to you...


      PS: This node was written (mostly)using your editor:)

        Your analysis is quite correct Scott. That feature was jammed in just before I posted the code immediately before leaving on holiday over Easter so the gap hasn't been fine tuned. However I suspect it may be influenced by things such as the default font being used by the Text widget and perhpas by various system settings and display resolution.

        A number of things such as that could be made user preferences when the code is more fully developed.

        Glad to hear that you are using it for real BTW - Kudos to you :). (It's really a rather preliminary version.)

        DWIM is Perl's answer to Gödel
      scrape the node being replied to (to allow quoted material from the OP)

      Yeah, that's a good idea. My code (above) is only useful for editing extant nodes.

      We're building the house of the future together.
Re^2: PerlMonks Editor, console editor wrapper, unix strain (pm_vi)
by jakobi (Pilgrim) on Oct 03, 2009 at 15:41 UTC

    My thanx for the console offline editor (and also to GrandFather for this thread).

    I've slightly modified it for Unix, which will hopefully be forgiven by jdporter. It should still support Windows, just as before. And until I find a willing victim, I'll keep this version working

    For my personally, the main advantage of this solution over PMEdit is that the script wraps around your standard editor, and I thus can avoid TK and WYSIAYCEHTG.

    It's also quite useful to take a peek at the PM-html source of a node, e.g. how the heck did that fellow monk manage a blue background (A: class=settings_key; grey is class=readmore)? Especially nice with vim highlighting and the original whitespace (alternatively follow the xml link or add ;displaytype=xml to the URL).

    Changes include:

    • .netrc-support for credentials plus use of $EDITOR (set e.g. to vi, gvim, or gvim.exe)
    • skip upload if temporary file is unchanged
    • allow use of http urls, w/o protocol, and non-numerical 'node='-type id's
    • 20091004: be a bit less obscure on a missing -id. Some better guesses for a windows _netrc, and a notion of shortcuts (e.g. self for this very node; short cut are builtin or in a user-defined file $0.short). For backup, both the original and the new editor buffer are kept in /tmp (un-versioned, un-dated). This pretty much implements the rest of what I perceived as missing features.

    Possible Todos:

    • add options to just read or write to allow use in vim autocommands (e.g. vimdiff or reading two or three nodes into the same buffer; and probably also very helpful for the everything-in-one-vim fraction, which I don't belong to -> very low pri)

    Known Bugs:

    • I don't see any way to use this script with the private scratchpad, as displaymode=private and viewmode=xml within an URL seem to clash (server side, not pm_vi).
    • changes to home node or scratchpad are silently discarded by PM.
    • no idea what the corresponding _netrc location on windows would be. Search for ZZZ to update my guesswork. Or change some profile for cmd to provide $ENV{HOME} and place a .netrc (or _netrc) file in this dir.
    • AFAIK has 64K limits on at least some of its nodes. Prod me if there's a need to replace the (.*) regex capture group with real code; but much to my enjoyment, the 64K regex pattern/capture-group 'stretch' limit seems to be ancient history (since at least 5.8, as tested with a simple ([\s\S]*)).
      Currently the script prints out a paranoid 20K reminder asking the user to double-check larger nodes (20K may well expand upto the 64K range with HTML entities).
    • 20091009: title entity encoding issue fixed (I think)

    A diff -u is a bit long compared to the script (given that I touched only 3 locations, excluding whitespace, comments and 2 variables...), so here's the script in full:

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2022-08-08 07:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found