Here is an example of a very long post title:
Braveheart is an Academy Award-Winning 1995 historical action-drama movie produced and directed by Mel Gibson who also starred in the title role
This title is 144 characters long, 4 more than the maximum allowed by Twitter. If you were to use the default format string New post: !title !url the Twitter message would look like this:
New post: Braveheart is an Academy Award-Winning 1995 historical action-drama movie produced and directed by Mel Gibson who also starred in ...
This is bad because the URL is omitted from the message. Ideally, in the case of long titles, we would want to truncate the title and keep the TinyURL:
New post: Braveheart is an Academy Award-Winning 1995 historical action-drama movie produced and directed by ... http://tinyurl.com/aj8u2v
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | twitter_truncation_4.patch | 5.46 KB | todd nienkerk |
| #6 | twitter_truncation_3.patch | 5.67 KB | todd nienkerk |
| #3 | twitter_truncation_2.patch | 6.29 KB | todd nienkerk |
| #1 | twitter_truncation.patch | 6.47 KB | todd nienkerk |
Comments
Comment #1
todd nienkerk commentedThe attached patch does many things, all of which are needed to provide good truncation logic.
It's kind of confusing, so any help making it more concise or efficient is much appreciated.
Comment #2
todd nienkerk commentedThe patch applies to the 6.x-2.x-dev snapshot, not the 6.x-2.0 release.
Comment #3
todd nienkerk commentedI've attached a better, more streamlined patch. Logic is simpler, and there are fewer vars.
Patch applies to 6.x-2.x-dev 2009-Jan-17 (not against my previous patch).
Comment #4
eaton commentedTodd, good catch on the title length issue. I'm a little concerned, though, that all of this extra code is essentially what token module provides; it was designed explicitly for this sort of thing, and already handles extracting the data we're talking about pulling up.
I'd much prefer to just add optional support for node tokens to the outgoing post, and expose a 'tinyurl' token for all nodes. Would there be objections to that approach?
Comment #5
todd nienkerk commented@Eaton: Yeah, it's a lot of code. There's a lot of logic to it because it involves predicting how many characters will be available after all
!stringsare replaced. I'm not a seasoned developer, though, so I'm sure you can come up with a more efficient way to do this!I have no objects to a token approach so long as it can:
!stringsremovedOne question regarding the token approach: Wouldn't it be better to build a tinyurl module that provides the token? Maybe a module that supports multiple URL-shortening services? tinyurl is losing mindshare to other, more concise services: bit.ly, is.gd, etc. Here's an annoyingly long list of such services.
PS. We're using the patched version of the module on the Four Kitchens site: twitter.com/fourkitchens. We're happy to test any improvements or alternative methods.
Comment #6
todd nienkerk commentedRe-rolled patch for the 2009-02-10 snapshot of 6.x-2.x-dev.
Comment #7
gregglesMy initial reaction when I saw the title of this issue was "all we need is a [title-X-chars] token that has reasonable X values like 20, 30, 50, and then that token takes care of truncating/ellipses.
Looking at the discussion and proposed patch here...that would indeed be a lot less code. It does require the administrator to be somewhat smart when they setup the pattern to make sure that their tokens will all work, but...that's why they get paid the big bucks to be the admin, right?
I think that it would make sense for twitter to have a hook just after the message has been built (which would receive the pattern, the tokens, and the current replacement) so that another module could jump in and do really smart things like truncating specific tokens to fit better. Putting all that logic into token seems hard because the "right thing to do" depends on so many variables...
Comment #8
Leeteq commentedSubscribing.
Comment #9
todd nienkerk commented@greggles:
I absolutely agree there's potential for Token to handle much of the grunt work. But whatever the solution, this module -- which I use all the time and love! -- must "protect" the URL. After all, it's the most important part of any "check out my new post"-style tweet.
The rules for creating a successful Drupal post tweet are:
(1) Create the shortest URL possible to maximize the number of other characters in the tweet. (In some rare cases, this may be the original URL. Usually, the TinyURL is more efficient.)
(2) Calculate the number of non-URL and non-title characters are in the tweet, including spaces that must separate the elements. Example: "New post!"
(3) Fit the post's title into the remaining gap. Bonus points for truncating between words instead of mid-wo...
Comment #10
todd nienkerk commentedRe-rolled patch for 6.x-2.3.
Comment #11
michaek commentedComment #12
bkosborneWhy was this set to won't fix?
Comment #13
gregglesAgreed.
Comment #14
steinmb commentedAgree, moving to dev. Anyone feel up to rerolling this?
Comment #15
davidneedhamIt's been awhile, but this still seems valid.
Possible cross-reference: #1441596: Add support for tokens module on twitter post.
Comment #16
davidneedhamOops, wrong status.
Also, as I understand it the code was re-written significantly since 6.x-3.x, so it needs to be rolled up before it'll be ready for review.
Comment #17
13rac1 commentedPatch does not apply to 6.x-5.x.
Comment #18
xurizaemonRe-roll welcome, but Twitter does its own shortening stunts now so let's drop the tinyurl remnants if there are any.