SorrybutAVG

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, July 1, 2009

Reinventing the Wheel

Posted on 1:45 PM by Unknown
There were a couple of web-related things I recently puzzled through before finding out somebody had already solved the problem, but I'm kind of proud of them anyway.

The first one was a Perl script to convert from LiveJournal's export format to the Atom format for import into Blogger. I wrote it so my wife could transfer her old LiveJournal blog. After some finagling, I even got it to import the comments correctly and get the timestamps right.

I later found out some other guy had actually written a much more full-featured application for managing and converting LiveJournal posts. The punchline, though, is that he also wrote it for his wife. Ba-dump!

The second cool thing I figured out you can see by clicking on the link below.

Woo hoo! Now, I know everybody's blog already has this, but I ended up doing my own homebrew solution without peeking. It was doubly satisfying because I didn't know any Javascript prior to this, and I ended up with a JS-heavy solution.

Blogger's help tells you how to do a rudimentary fold by defining a style called "fullpost" that doesn't do anything if you are on a post page, but causes the text not to be displayed if you are anywhere else (e.g. like on the main page). So then you just wrap everything below the fold in a <span class="fullpost"> tag and that makes it appear in posts but disappear on the main page.

The problem is, they then have you put a fixed "Continue reading..." link in the HTML that appears at the bottom of every single post on the main page. So either you have a fold in every post (blech!) or else you deal with having a "Continue reading..." link that doesn't actually give you any more content (double-blech!). They say that fixing this is left "as an exercise for the reader".

My first solution did not involve any Javascript, but instead used a conditional based on Blogger's custom tags. I had it scan the labels for each post, and if it found the label "more", then it inserted the "Continue reading..." link. Note that it used no standard scripting language, only the custom tags provided by Blogger, meaning that all of this was executed on the server-side. The two big disadvantages of this approach is that a) I have to remember to add the label to every post where I use the "fullpost" span, and b) the label "more" was showing up all over the place. I partially solved the second problem by modifying the code for listing the labels so as to forcibly exclude "more", but this occasionally messed up the formatting of the labels, and also caused some weird bug in the tag cloud code that I ganked.

So on to Javascript! First, I changed the HTML for each post entry on the main page to wrap the entire post in a span tag with an id that matched the post ID. Then, I added a script that executes at the bottom of each page. The script grabs the HTML DOM element with the id matching the post ID, then searches that element for span tags, and compares the class of each to "fullpost". When it finds a match, it inserts the "Continue reading..." link. Voila! Now, any time I use the "fullpost" span, your browser figures out I did it and adds the link. Nice.

The finishing touch I added today. In many blogs, the "Continue reading..." link will not only take you to the post page, but will jump to an anchor marked right where you left off. I wanted it to do this, but not have to actually add the anchor in myself, i.e. I wanted it to somehow put the anchor in for me wherever I put the "fullpost" tag in.

It would be nice if CSS allowed you to put macros in for certain classes, i.e. if I could just tell it the text I wanted to appear whenever I had something of class "fullpost". But no dice.

So instead, since I had code to find the DOM element corresponding to "fullpost" span tag anyway, I decided to see if I could just modify the DOM tree to insert the anchor. I thought I might have to do something fancy, like instantiate an anchor element and insert it into the linked list. But I forgot that Javascript is, well, a scripting language and not a programming language, so they try to make it intuitive. And sure enough:

spanElem.innerHTML = "<a name='more'/>" + spanElem.innerHTML;

Yep, that was it. Just access the damn HTML and prepend a string to it. Too simple!

After I wrote that, it occurred to me that I wasn't sure if the Javascript would execute before or after the browser searched for the anchor tag, but it seems to work in Chrome. Ping me if it fails in any other browsers.

The only thing that worries me is that it's doing a lot of important formatting on the client side... it seems like putting anchors and links in place ought to be done server side, or at least that's what it feels like to a non-web developer like me. The pages still seem to load pretty fast, though, and it's not like you can view Blogger without Javascript support anyway, so I think it's okay. Hmm, maybe when I said "ping me if it fails in any other browsers," I should have mandated that they be, you know, modern browsers. I just know somebody is going to be like, "It doesn't work in Links!"
Email ThisBlogThis!Share to XShare to Facebook
Posted in programming | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • I win an argument with Dan Cooper!
    I won't bore anyone with the details, but I just totally whupped Dan Cooper (yes, that Dan Cooper) in an argument on Google+ . After I...
  • God takes sides in Survivor: Samoa
    My wife and I were watching Survivor: Samoa last night (yes, I kind of like that show) and a reward challenge involved a hilarious scene wh...
  • Ten New Ways to Piss Off God
    Found a new one today. Leviticus 21:18-20, while not explicitly condemning anyone, bars the following individuals from "approaching th...
  • I feel bad for George Zimmerman, I really do
    The latest news has George Zimmerman completely flipping out and ditching his lawyers, putting together a poorly-designed website 1 , and t...
  • About that Time cover...
    So yeah, everybody's seen it , right? A lot of my Facebook friends are nursing moms, a lot of them are AP moms, a few of them are even ...
  • The disastrous ethical consequences of the Atonement meme
    It has been pointed out by Hitchens and others that the idea of Jesus suffering and dying for our sins is not just repulsive because it...
  • Before, During, and After
    I don't usually blog much about home improvement , but I figured I ought to do a follow-up post to the one about venturing into the craw...
  • The Archdiocese of Washington and Pat Condell
    Earlier today I watched the newest Pat Condell rant by following the link at richarddawkins.net . Then later, I happened to stumble on thi...
  • I guess the Birthers were right all along
    Ah hah, here is the proof that Obama is not an American citizen after all! On a side note, take a gander at the ad for Carnation infant for...
  • Faith is Nihilism
    Bryan Fischer says that not exploiting fossil fuels to the absolute maximum is like rejecting a birthday present from Jesus, and that if we...

Categories

  • abortion
  • accomodationism
  • alternative medicine
  • apatheism
  • atheism
  • birther hilarity
  • bus ads
  • censorship
  • christian apologetics
  • christianity
  • civility
  • colbert
  • cooking
  • creationism
  • death
  • drinking
  • dualism
  • environment
  • evolution
  • facial hair
  • faith
  • feminism
  • file sharing
  • health care
  • hinduism
  • history
  • home improvement
  • intelligent design
  • islam
  • judaism
  • lgbt
  • local farming
  • lolcatz
  • math
  • misogyny
  • morality
  • mormonism
  • music
  • occupy
  • parenthood
  • peer review
  • philosophy
  • physics
  • politics
  • programming
  • racism
  • sikhism
  • skepticism
  • sustainable farming
  • trolls
  • vaccines
  • vegetarianism
  • video games
  • war on christmas

Blog Archive

  • ►  2013 (2)
    • ►  April (1)
    • ►  February (1)
  • ►  2012 (55)
    • ►  December (1)
    • ►  November (3)
    • ►  October (1)
    • ►  September (1)
    • ►  August (3)
    • ►  July (2)
    • ►  June (8)
    • ►  May (9)
    • ►  April (7)
    • ►  March (10)
    • ►  February (4)
    • ►  January (6)
  • ►  2011 (72)
    • ►  December (4)
    • ►  November (7)
    • ►  October (2)
    • ►  September (4)
    • ►  August (4)
    • ►  July (4)
    • ►  June (1)
    • ►  May (3)
    • ►  April (6)
    • ►  March (17)
    • ►  February (10)
    • ►  January (10)
  • ►  2010 (106)
    • ►  December (7)
    • ►  November (4)
    • ►  October (10)
    • ►  September (11)
    • ►  August (15)
    • ►  July (7)
    • ►  June (10)
    • ►  May (5)
    • ►  April (14)
    • ►  March (10)
    • ►  February (5)
    • ►  January (8)
  • ▼  2009 (171)
    • ►  December (10)
    • ►  November (5)
    • ►  October (19)
    • ►  September (26)
    • ►  August (38)
    • ▼  July (52)
      • RU8BIT?
      • Finally, a piece of Islamic oppression that I can ...
      • Smooth Inventor
      • Steve Ballmer hypocrisy FAIL
      • I guess the Birthers were right all along
      • The uncomfortable similarities between Religulous ...
      • Could God coin a word so self-contradictory, even ...
      • Joel Grus on apatheism
      • Beer can duck!
      • Let's play "Guess the Intolerant Religion!"
      • The Coexist? Comedy Tour
      • More pizzas
      • Can I demand an atheist police officer?
      • There is no such thing as secular totalitarianism
      • The Economist nails the accomodationism debate alm...
      • Techno wedding march
      • Test post 2
      • Internet Explorer Epic Fail
      • Blogging via text message
      • Results of ScienceBlogs upgrayedd
      • Bad news from the UK
      • Wright is wrong on evolution
      • Please do not read Romans 14:8 at my funeral
      • The PZ Effect (He's no Slashdot...)
      • What atheism is not
      • I dream of suing genie
      • The "Other" Accomodationism: Are the Abrahamic rel...
      • A new way to eat your greens
      • ZOMGitsCriss tells it like it is
      • Onward, Ninja Horde!
      • My opinion on Accomodationgate
      • Mormonism's central tenet: If you believe it's tru...
      • Oh noes! I have awoken the sleeping cephalopod!
      • Don't Waste Your Cancer!
      • "Everybody must like the same things I do, or else...
      • Pizzas everywhere!
      • Yet another tragic wrist injury
      • My New Favorite Blog
      • Abstract Horror becomes Actual
      • One of the Lucky Ones
      • She's Gone.
      • Get Well Kitty
      • Rain of Spit
      • Another Big Pharma Conspiracy!
      • Either really good news, or really bad news
      • Time to train the dogs...
      • Lamb Illustrated
      • Reinventing the Wheel
      • Wherein I prove my ineptitude with a jigsaw...
      • When am I going to start remembering to take pictu...
      • Enough with the "Monkey" Business
      • The "Gish Gallop"
    • ►  June (21)
Powered by Blogger.

About Me

Unknown
View my complete profile