Drupalize image paths

by timmah

HTML

<div class="doublewide nomarginbottom">
		<h1>About NCIS</h1>
		<div class="margintop marginbottom box-dotted2 noprint" id="table_of_contents">
		<span id="label">Jump to:</span>
		<span class="entry"><a href="#charter" class="nounderline">NCIS Charter</a></span>
		|
		<span class="entry"><a href="#reconciliation" class="nounderline">Reconciliation at NCIS</a></span>
		|
		<span class="entry"><a href="#related" class="nounderline">Related programs &amp; sites</a></span>
	</div>
	</div>	
	<div class="doublewide nomargintop">
		<div class="w-narrow left marginright padbottom padtop"><img src="_lib/img/MDpressclub170209_200px.jpg" alt="" /></div>
		<p>The National Centre for Indigenous Studies (NCIS) was established in January 2005. It is a stand-alone Centre within the <a href="http://about.anu.edu.au/governance-structure/university-structure">ANU organisational structure</a> and is directed by <a href="/people/dodson.php">Professor Mick Dodson AM</a>.</p>
		<p>The <a href="/about_us.php#charter">Centre's charter</a> is for NCIS to be recognised as a leading academic institute for inter-disciplinary research in fields that are of relevance to Indigenous Australians, especially in relation to the enrichment of scholarly and public understandings of Australian Indigenous cultures and histories. NCIS works collaboratively with ANU research and teaching centres that are of relevance to Indigenous studies. These include the <a href="http://caepr.anu.edu.au/">Centre for Aboriginal Economic Policy Research (CAEPR)</a>, the <a href="http://acih.anu.edu.au/">Australian Centre for Indigenous History</a>, the <a href="http://archanth.anu.edu.au/">School of Archaeology and Anthropology</a>, and the <a href="http://music.anu.edu.au/">School of Music</a>.</p>
		
		<p>NCIS has an active commitment to the <a href="/about_us.php#reconciliation">reconciliation process</a>, through its initial involvement in the development of the ANU Reconciliation Action Plan, and through its...

JavaScript

/*  This script replaces file paths with Drupal equivalents, 
 *  assuming files are stored under /sites/default/files
 *
 * To use, paste the page SOURCE (Ctrl + U) - not the DOM - into the box above, 
 * and click 'Run' in the top right corner. 
 * 
 * The HTML you want is everying inside <div id="content">, starting with:
 *
 *     <!-- START MAIN PAGE CONTENT -->
 *
 * and ending at:
 * 
 *     <!-- END MAIN PAGE CONTENT -->
 * 
 */


// First, strip any unwanted elements

var needsNewPaths = $('img, a[href$=".bmp"], a[href$=".doc"], a[href$=".docx"], a[href$=".flv"], a[href$=".gif"], a[href$=".jpg"], a[href$=".jpeg"], a[href$=".mp3"], a[href$=".mp4"], a[href$=".mpg"], a[href$=".mpeg"], a[href$=".png"], a[href$=".pdf"], a[href$=".ppt"], a[href$=".pptx"], a[href$=".rtf"], a[href$=".wma"], a[href$=".zip"]'),
	unwantedBits = $('h1, a.scrollup');

unwantedBits.remove();


// Check each image, replace file path

needsNewPaths.each(function() {
	var $this  = $(this),
		$attr = function() {
				// If the element is an image...
					if ($this.is('img')) {
						return 'src'
				// else assume it is an anchor. Add more conditions if more element types are added to 'needsNewPaths'
					} else {
						return 'href'
					}
				}
		
		// Split attribute path
		$parts = $this.attr($attr()).split('/'),
		
		// Get the file, which will be the last item in our Split array
		$file = $parts[$parts.length -1];

/* // Test values here
	console.log('$parts: ' + $parts);
	console.log('$file: ' + $file);
*/	

	// Assign the new paths
	$this.attr($attr(), '/sites/default/files/' + $file);
});