User:Fæ/BM refs/imacro

From Wikipedia, the free encyclopedia

Description[edit]

British Museum iMacro script to extract information from on-line collection records.

Ensure that:

  1. the script code is saved as a JavaScript (.js) file rather than a default iMacro (.iim).
  2. you are running iMacro in Firefox.
  3. you are looking at a page from the BM collections database showing a record about an artefact (not the same thing as a BM article or highlights page).

The normal policies for use of templates apply, results may need copy editing before use.

Please contact User talk:Fæ for reporting bugs (include a sample url) and suggesting improvements.

Example output[edit]

Example result cut & paste from browser alert
British Museum infobox

{{infobox artefact
|name = human mummy
|image =
|image_caption =
|material = human tissue
|size = Length: {{convert|163|cm|in|abbr=on}}
|writing = 
|created = 3400BC (c.)
|period = Naqada II, Late Predynastic
|place = Gebelein
|location = G64/15, [[British Museum]], London
|id = 32751
|registration = {{British-Museum-db|1900,1018.1|id=117645}}
}}

Footer

{{British-Museum-object|32751 / Registration:1900,1018.1|id=117645}}

Link

{{British-Museum-db|32751 / Registration:1900,1018.1|id=117645}}

Script code[edit]

/* iMacro File:BM_object.js
   Contact: http://en.wikipedia.org/User_talk:Fæ
   Created: 11 June 2010
   Last edit: 14 June 2010

   Description:
   Wikipedia - British Museum script.
   Script to extract values from records on the on-line British Museum collections
   database and display {{infobox artefact}} and other templates.
   Text is displayed in a browser alert and can be cut & paste into articles.
   
   Warnings:
   - Do not paste into articles willy-nilly, templates must be appropriate.
   - Ensure the information extracted displays correctly and is relevant to the
     article. The script may extract more data than is useful, be prepared to trim or
     copy edit the text.
*/

/* bmget: Get a value
   Find text with a matching attribute (such as class, style or text value).
*/
// T is HTML tag, A is attributes to find and H is "TXT" or "HTM" for the format required
function bmget(T,A,H){
if(H==null){H="TXT"};
iimPlay("CODE:\n\
' "+T+"\n\
SET !TIMEOUT_TAG 1\n\
TAG POS=1 TYPE="+T+" ATTR="+A.replace(/ /g,"<sp>")+" EXTRACT="+H+"\n\
");
return iimGetLastExtract()=="#EANF#"?"":iimGetLastExtract();
}

/* bmgetset: Get set of values
   These look like "<strong>title</strong>value 1<br>value 2<br>".
*/
// T is HTML tag, A is title text to find
function bmgetset(T,A){
iimPlay("CODE:\n\
' "+T+"\n\
SET !TIMEOUT_TAG 1\n\
TAG POS=1 TYPE="+T+" ATTR=TXT:*"+A.replace(/ /g,"<sp>")+"* EXTRACT=HTM\n\
");
var r=iimGetLastExtract().replace(/<\/?a .*?>/g,"").replace(/<\/?p>/g,"").split(A)[1];
if(r==""||r==undefined){return ""};
r=r.split(/<.?strong>/)[1];
if(r==""){return r};
var rArr=r.split("<br>");
rArr.shift();
r=rArr.join(", ").replace(/ \((scope|all).*?\)/g,"")
 .replace(/Found.acquired /i,"")
 .replace(/[\n\r]/g,"").replace(/,\s*$/,"");
return r;
}

/* Main code */

// Find objectId (no easy way for iMacro to access the window.location)
var objectId=bmget("DIV","ID:*researchObject*","HTM").replace(/^.*objectId=(\d{2,}).*$/i,"$1");
if(objectId.length>15){objectId=""};

// Get values
var result="{{infobox artefact";

// ..General object
result+="\n|name = "+bmget("H1","CLASS:noBliss");
result+="\n|image =\n|image_caption =";
var material=bmgetset("P","Materials");
result+="\n|material = "+material.substr(0,1).toUpperCase()+material.substr(1);
result+="\n|size = "+bmgetset("P","Dimensions")
  .replace(/([\d\.]+) centimet..s?/gi,"{{convert|$1|cm|in|abbr=on}}")
  .replace(/([\d\.]+) millimet..s?/gi,"{{convert|$1|mm|in|abbr=on}}")
  .replace(/,\s*(\w+):/g,"<br>$1:");

// ..Inscriptions
var inscr=bmgetset("P","Inscriptions");
result+="\n|writing = "+Array(
   (inscr.search(/Inscription Script/i)>-1?inscr.replace(/^.*Inscription Script: ([\w ]+).*$/mi, "$1"):""),
   (inscr.search(/Inscription Lang/i)>-1?inscr.replace(/^.*Inscription Language: ([\w ]+).*$/mi,"$1"):"")
   ).join(", ").replace(/^, |, $/g,"");

// ..Dates and period
result+="\n|created = "+bmgetset("P","Date").replace(/(\w)(\()/g,"$1 $2");
var period=bmgetset("P","Period")
  .replace(/(Viking)/i,"[[Viking Age|$1]]")
  .replace(/(Romano-British)/i,"[[Romano-British culture|$1]]")
  .replace(/(Anglo-Saxon)/i,"[[Anglo-Saxons|$1]]");
if(period!==""){result+="\n|period = "+period};
result+="\n|place = "+bmgetset("P","Place").replace(/Excavated.Findspot /ig,"")+bmgetset("P","Production place");

// ..Current info
var loc=bmget("P","TXT:*Location:*").replace(/^.* ([^\s]*)$/,"$1").replace(/^G(\d{1,})\/.*/,"Room $1");
result+="\n|location = "+(loc==""?"":loc+", ")+"[[British Museum]], London";

// ..Id
var regNo=bmget("P","TXT:*Registration number*").replace(/^.*?(\d{4}[\d,\.\-\s]+)/,"$1").replace(/.*Registration number. */,"");
var bigNo=bmget("P","TXT:*Big number*").replace(/^.*?(\d{4,})$/,"$1");
if(bigNo==""){bigNo=bmgetset("P","Additional ID")};
if(regNo==bigNo){regNo=""};
result+="\n|id = "+(regNo==""?"{{British-Museum-db|"+bigNo+"|id="+objectId+"}}":bigNo);
if(regNo.length>2){result+="\n|registration = {{British-Museum-db|"+regNo+"|id="+objectId+"}}"};
  
result+="\n}}\n\n";

// Footer template
var dispNo='';
if(bigNo.length>2){
  dispNo=""+bigNo;
  if(regNo.length>4){dispNo=""+bigNo+" / "+"Registration:"+regNo};
}else{
  dispNo=regNo;
}
result+="Footer\n\n{{British-Museum-object|"+dispNo+"|id="+objectId+"}}\n\n"+
  "Link\n\n{{British-Museum-db|"+dispNo+"|id="+objectId+"}}\n\n";
  
// Display result
alert("British Museum infobox\n\n"+result);