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

  • If feminism killed chivalry, then chivalry kills common courtesy
    An argument that those who pine for the days of unrepentant misogyny are fond of repeating is the idea that gender equality has eroded tradi...
  • Gender politics and Automoblox
    Update: Jason Thibeault, who has a much larger readership than I, helped me publicize this issue (thanks Jason!)... and the co-founder sho...
  • Free will and hot peppers
    Daniel Dennett and Jerry Coyne are hanging around the former's apartment. Dennett goes to the kitchen, pulls a jalapeno from the coldes...
  • Why Cee Lo's word change to Imagine is really crappy
    I was not the only one to notice that Cee Lo Green, when performing John Lennon's Imagine just before the ball drop last night, change...
  • If you are liberal or atheist and you oppose the Park51 community center, you have been bamboozled
    It's infuriating, but probably not too surprising, how many conservatives don't understand the First Amendment. Regardless of how y...
  • Why I oppose the figurative use of the term "militant" in all contexts
    Uh oh, I go and call Bruce Hood a "nice guy" , and Bruce himself responds in the comments unfortunately triggering one of my big ...
  • Why is oxygen the third most common element in the universe?
    So it appears from several sources that the three most common elements in the universe are, in order, hydrogen, helium, and oxygen. The firs...
  • The evolutionary implications of our preference for the Maillard reaction
    This is something that has been bugging me the last couple of days... As many of the foodies out there may know, the Maillard reaction (pr...
  • Sorry, but AVG Secure Search is malware (Update: but maybe won't be in the next version)
    Discovered AVG Secure Search today when I installed the trial version of WinZip on a laptop that I only intend to use temporarily. There se...
  • Why the experience of anti-atheist prejudice is different, though similar, to prejudice regarding sexual orientation, race, gender, etc.
    At a post over at Prop8TrialTracker.com on the purging of the most vile homophobic elements from the mainstream anti-marriage equality move...

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