Skip to content

Instantly share code, notes, and snippets.

@joshkehn
Created May 24, 2012 17:28
Show Gist options
  • Save joshkehn/2782936 to your computer and use it in GitHub Desktop.
Save joshkehn/2782936 to your computer and use it in GitHub Desktop.
Convert flickr share blobs into nicely formatted markdown and source link output.
var chunks = ['<a href="http://www.flickr.com/photos/joshkehn/7262091846/" title="Fireboat by joshkehn, on Flickr"><img src="http://farm8.staticflickr.com/7086/7262091846_80127b652c_z.jpg" width="640" height="427" alt="Fireboat"></a>',
'<a href="http://www.flickr.com/photos/joshkehn/7262095890/" title="Pier by joshkehn, on Flickr"><img src="http://farm8.staticflickr.com/7215/7262095890_19a1d5059f_z.jpg" width="427" height="640" alt="Pier"></a>'];
var title_keys = {};
var format_lines = [];
var link_lines = [];
var img_lines = [];
chunks.forEach(function processChunk (chunk) {
// Link chunk
var p1 = chunk.split('href="');
var href = p1[1].split('" ')[0];
// Title chunk
var p2 = chunk.split('alt="');
var title = p2[1].split('">')[0];
var ltitle = title.toLowerCase();
// Image source chunk
var p3 = chunk.split('src="');
var img_src = p3[1].split('" ')[0];
// Avoids duplicate keys
if (typeof title_keys[ltitle] === "undefined") {
title_keys[ltitle] = 0;
} else {
title_keys[ltitle] += 1;
ltitle += title_keys[ltitle];
}
format_lines.push('[!["' + title + '"][flickr img: ' + ltitle + ']][flickr: ' + ltitle + ']');
link_lines.push('[flickr: ' + ltitle + ']: ' + href);
img_lines.push('[flickr img: ' + ltitle + ']: ' + img_src);
});
format_lines.forEach(function printFormatLine (l) {
console.log(l);
});
console.log('\n<!-- Flickr href links -->');
link_lines.forEach(function printLinkLine (l) {
console.log(l);
});
console.log('\n<!-- Image source links -->');
img_lines.forEach(function printImgLine (l) {
console.log(l);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment