Arc Forumnew | comments | leaders | submitlogin
HN patch: add quotes to comment boxes
3 points by zck 4686 days ago | 4 comments
In HN, there's no syntax-supported way to quote someone. You can put two spaces in front of a quote, but that doesn't line-wrap. You can put ">" in front, but that isn't as obvious at a glance as one would like.

I've created a patch which adds quote support from the markdown spec; if you put > at the beginning of a line, it'll wrap <blockquote> tags around all text until there's a blank line. The necessary functions are in app.arc . 'markdown needs to be changed. I've annotated the diffs:

  (def markdown (s (o maxurl) (o nolinks))
    (let ital nil
      (tostring
        (forlen i s
          (iflet (newi spaces) (indented-code s i (if (is i 0) 2 0))
            (do (pr  "<p><pre><code>")
                (let cb (code-block s (- newi spaces 1))
                     (pr cb)
                     (= i (+ (- newi spaces 1) (len cb))))
                (pr "</code></pre>"))
            (if (quote-begins-at s i) ;;quote code begins here
              (let end (quote-end s i)
                 (pr "<blockquote>"
                     (get-quote s (+ i 1))
                     "</blockquote>")
                 (= i (+ 1 (or end len.s)))) ;;quote code ends here
              (iflet newi (parabreak s i (if (is i 0) 1 0))
                (do (unless (is i 0) (pr "<p>"))
                    (= i (- newi 1)))
                (and (is (s i) #\*)
                     (or ital
                         (atend i s)
                         (and (~whitec (s (+ i 1)))
                              (pos #\* s (+ i 1)))))
                (do (pr (if ital "</i>" "<i>"))
                    (= ital (no ital)))
                (and (no nolinks)
                     (or (litmatch "http://" s i)
                         (litmatch "https://" s i)))
                (withs (n   (urlend s i)
                            url (clean-url (cut s i n)))
                  (tag (a href url rel 'nofollow)
                    (pr (if (no maxurl)
                          url
                          (ellipsize url maxurl))))
                  (= i (- n 1)))
                (writec (s i)))))))))
And three functions need to be added:

  (def quote-begins-at (str i)
       "true if a quote begins at str.i"
       (and (or (is i 0)
                (is (str (- i 1)) #\newline))
            (begins str "&#62;" i)))
  
  (def quote-end (str i)
       "Returns the end of a quote beginning at i"
       (posmatch "\n\n" str i))
  
  (def get-quote (str i)
       "Returns the substring beginning at i and ending the next time there are two #\newline s in a row; failing that, the end of the string"
       (cut str (+ i 4) ;;get past &#62; which is the beginning of the quote
              (quote-end str i)))
It doesn't support nested quotes, or any markdown syntax inside the quote.

I'll license this code under the same license as Arc (Perl Artistic 2.0), in case anyone actually wants to use it. Please let me know if you do.



1 point by zck 4685 days ago | link

Also, apparently I'm dumb, and typoed the title to this post; it should be "add quotes to comment boxes".

And I reread the body multiple times...

-----

2 points by akkartik 4685 days ago | link

I fixed it :)

Also, may I suggest indenting lines by just two spaces rather than the length of the first word. It would alleviate the horizontal scrolling on the page. Let me know if you're ok with it, and I'll fix it for you.

(I'm not presuming to suggest how you should indent your code in general, of course.)

-----

1 point by zck 4685 days ago | link

Awesome, thanks!

Please go ahead and fix it. The indentation here is a direct result of Emacs's default Lisp indentation settings. I haven't bothered to change it, both because it feels difficult, and because I keep vacillating between which way is actually better. But that's another discussion entirely.

-----

2 points by akkartik 4685 days ago | link

Done. Oh, I just realized most of that code has the same indentation as the original release of arc back in 2008. So it wasn't your editor at all!

(So pg/rtm indented iflet like and? That is most eccentric.)

-----