// ==UserScript==
// @name           Images
// @namespace      http://everything2.com
// @description    Embed linked images in writeups.
// @include        http://everything2.com/*
// @include        http://www.everything2.com/*
// @include        http://everything2.net/*
// @include        http://www.everything2.net/*
// @include        http://everything2.org/*
// @include        http://www.everything2.org/*
// @author         raincomplex
// ==/UserScript==

/*
    NOTE: Only users of this script will be able to see the images,
          so be sure to also link the SOURCE page somewhere in your writeup
          for those who aren't in the know.
          (And optionally add a link to this script at the beginning.)
    
    NOTE: You must use the raw HTML editor, not the WYSIWYG editor.
    
    To add images to your writeups, add lines like this:
    
        <!-- image: DIRECT SOURCE -->
    
    Where DIRECT is a URL directly linking to the image,
    and SOURCE is a URL to the page containing the image (the context).
    SOURCE is optional, but encouraged.
    
    You can also use leftimage, centerimage, or rightimage.
    The default (just 'image') is equivalent to rightimage.
    
    You can also use <!-- clear --> to move whatever comes next down
    below any images which may be above it.
    (It inserts an empty div with style "clear: both".)
    A centerimage implicitly includes a clear.
    
    For examples of this in use, see these writeups:
        Can you show me?
        And the air would taste of patterns
*/

const maxwidth = '200px'
const maxheight = '150px'

var pagebody = document.getElementById('pagebody')

for (i = 0; i < pagebody.children.length; i++) {
    var div = pagebody.children[i]
    if (div.className.search(/(^| )item( |$)/) != -1) {
        var content = div.children[1]
        var html = content.innerHTML.replace(/<!--\s*clear\s*-->/gi, '<div style="clear: both"></div>')
        var regex = new RegExp('<!--\\s*(left|center|right)?\\s*image:\\s*([^\\s"]+)(\\s+[^\\s"]+)?\\s*-->', 'gi')
        var newhtml = ''
        var lasti = 0
        while (true) {
            var m = regex.exec(html)
            if (m == null) break
            newhtml += html.substr(lasti, m.index - lasti)
            lasti = m.index + m[0].length
            
            var side = m[1]
            var otherside = null
            if (side == null)
                side = 'right'
            if (side == 'left')
                otherside = 'right'
            if (side == 'right')
                otherside = 'left'
            
            var img = '<img style="max-width: ' + maxwidth + '; max-height: ' + maxheight + '; border: 0" src="' + m[2] + '">'
            if (m[3] != null) {
                // link to source page
                img = '<a href="' + m[3] + '">' + img + '</a>'
            }
            
            var cleardiv = ''
            var wrap1 = ''
            var wrap2 = ''
            var float = ''
            if (side == 'center') {
                float = 'clear: both; display: inline-block'
                wrap1 = '<center>'
                wrap2 = '</center>'
            } else {
                cleardiv = '<div style="clear: ' + side + '"></div>'
                float = 'float: ' + side + '; clear: ' + side + '; margin-' + otherside + ': 10px'
            }
            
            newhtml += cleardiv + wrap1 + '<div style="' + float + '; border: 1px solid black; padding: 2px; margin-bottom: 10px">' + img + '</div>' + wrap2
        }
        newhtml += html.substr(lasti, html.length - lasti)
        content.innerHTML = newhtml
    }
}

// vim: ts=4 et

Log in or register to write something here or to contact authors.