mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2012-06-14 04:19 am

[dw-free] Markdown ninja-commit (testing)

[commit: http://hg.dwscoalition.org/dw-free/rev/8434b75cccba]

Markdown ninja-commit (testing)

Discussion and hack time at YAPC::NA has caused me to implement this (hacky)
way of using Markdown on Dreamwidth. In short, if you make the first line of
your post be "!markdown", the rest of the post is interpreted by the
Text::Markdown module.

This also enables a version of the testing user-reference code that I am
playing with. This is the Twitter style of "@foo" referring to a user. You
can also do "@foo.livejournal.com" to refer to a LiveJournal user.

Most user tags that you would originally write like this:

<user name="foo" site="foo.com" />

Can, if Markdown is enabled, be this:

@foo.foo.com

NOTE: this is being committed to give people the chance to play with it if
they want. The implementation is subject to change after discussion and
testing.

MORE NOTE: this only applies to entries. We could add this to comments, but
haven't yet.

Patch by [staff profile] mark.

Files modified:
  • bin/checkconfig.pl
  • cgi-bin/LJ/CleanHTML.pm
--------------------------------------------------------------------------------
diff -r 4755bce40d0c -r 8434b75cccba bin/checkconfig.pl
--- a/bin/checkconfig.pl	Tue Jun 05 06:02:57 2012 +0000
+++ b/bin/checkconfig.pl	Thu Jun 14 04:24:39 2012 +0000
@@ -238,6 +238,10 @@
                    deb => 'libtheschwartz-perl',
                },
                "TheSchwartz::Worker::SendEmail" => {},
+               "Text::Markdown" => {
+                   deb => 'libtext-markdown-perl',
+                   opt => 'Required to allow using Markdown in entries.',
+               },
               );
 
 
diff -r 4755bce40d0c -r 8434b75cccba cgi-bin/LJ/CleanHTML.pm
--- a/cgi-bin/LJ/CleanHTML.pm	Tue Jun 05 06:02:57 2012 +0000
+++ b/cgi-bin/LJ/CleanHTML.pm	Thu Jun 14 04:24:39 2012 +0000
@@ -1476,6 +1476,28 @@
         $opts = { 'preformatted' => $opts };
     }
 
+    # this is the hack to make markdown work. really.
+    if ($$ref =~ s/^\s*!markdown\s*\r?\n//s) {
+        my $rv = eval "use Text::Markdown; 1;";
+        die "Attempted to use Markdown without the Text::Markdown module.\n"
+            unless $rv;
+
+        # first, markdown-ize the world
+        $$ref = Text::Markdown::markdown( $$ref );
+        $opts->{preformatted} = 1;
+
+        # second, convert @-style addressing to user tags
+        my $usertag = sub {
+            my ($user, $site) = ($1, $2 || 'dreamwidth.org');
+            if (my $siteobj = DW::External::Site->get_site( site => $site )) {
+                return qq|<user name="$user" site="$siteobj->{domain}" />|;
+            } else {
+                return qq|\@$user.$site|;
+            }
+        };
+        $$ref =~ s/(?<=\W)\@([\w\d_]+)(?:\.([\w\d\.]+))?(?=$|\W)/$usertag->($1, $2)/mge;
+    }
+
     my $wordlength = defined $opts->{'wordlength'} ? $opts->{'wordlength'} : 40;
 
     # fast path:  no markup or URLs to linkify, and no suspend message needed
--------------------------------------------------------------------------------
jeshyr: Blessed are the broken. Harry Potter. (Default)

[personal profile] jeshyr 2012-06-14 04:24 am (UTC)(link)
\o/

*happydance!*
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)

Sample Markdown comment

[personal profile] sophie 2012-06-14 06:32 am (UTC)(link)
!markdown
> Discussion and hack time at YAPC::NA has caused me to implement this (hacky)
> way of using Markdown on Dreamwidth. In short, if you make the first line of
> your post be "!markdown", the rest of the post is interpreted by the
> Text::Markdown module.

Oh, yay! It looks like it *should* apply to comments too, and although it's not live yet, I wanted to try making a comment in [Markdown](https://en.wikipedia.org/wiki/Markdown) so that when it does go live, this comment will update.

[EDIT: Yep, I had to use HTML to strike out that line there, because I missed this bit:

> MORE NOTE: this only applies to entries. We could add this to comments, but
> haven't yet.

I hope it does happen!]

One issue that I think may happen; it's possible that if the Markdown invocation implementation changes, that entries and comments using the old method will fail to display. So I don't recommend using this as anything but a test right now. If that happens, this comment will also probably fail to display properly, so it'll be obvious when this happens.

This is, of course, assuming we'll be supporting Markdown in comments too :)

Also, this code is awesome:

+ # second, convert @-style addressing to user tags
+ my $usertag = sub {
+ my ($user, $site) = ($1, $2 || 'dreamwidth.org');
+ if (my $siteobj = DW::External::Site->get_site( site => $site )) {
+ return qq|[Bad username or site: $user @ $siteobj->{domain}]|;
+ } else {
+ return qq|\@$user.$site|;
+ }
+ };

If I'm reading right, this will allow use of not only "@xb95" to link to a DW journal, but also "@xb95:livejournal.com" to link to another site, too.

---

The only problem I have with Markdown
is its unorthodox handling of newlines.
This paragraph is split up like a poem,
so with Markdown on, it'll look like
a normal paragraph
but with Markdown disabled,
you'll see the newlines.

It *is* possible to override this though,
as in this paragraph.
This paragraph will display as a poem
with or without Markdown enabled.
There are two spaces
at the end of each line.
We'll definitely want a good FAQ
on how to use Markdown
effectively.

---

And thus ends my Markdown test comment. :D I'm aware it won't display properly right now, though, as mentioned in the first paragraph :)
Edited (Adding more to the 'poem'.) 2012-06-14 06:43 (UTC)
gchick: Small furry animal wearing a tin-foil hat (Default)

[personal profile] gchick 2012-06-14 10:17 pm (UTC)(link)
ooooh, shiny.