Skip to content

Instantly share code, notes, and snippets.

@thewheat
Last active June 6, 2020 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewheat/a1ba7032615b0bb9fd68802b4b8e64db to your computer and use it in GitHub Desktop.
Save thewheat/a1ba7032615b0bb9fd68802b4b8e64db to your computer and use it in GitHub Desktop.
Demo file of http://annotatorjs.org/ that allows saving of annotations in a simple text file
<?php
define("ANNOTATIONS_FILE", "annotations.json");
$annotations = getAnnotations();
if (!$_GET || !$_GET['action']){
}
else if ($_GET['action'] == "search") {
$uri = $_GET['uri'];
$output = ['total' => 0, 'rows' => []];
foreach($annotations as $annotation){
if($annotation->uri == $uri){
$output['rows'][] = $annotation;
$output['total']++;
}
}
echo json_encode($output);
return;
}
elseif ($_GET['action'] == "create") {
$id = getGUID();
$json = readInputAsJSON();
$json->id = $id;
$annotations[] = $json;
saveAnnotations($annotations);
$output = ['status' => 'success', 'id' => $id];
echo json_encode($output);
return;
}
elseif ($_GET['action'] == "update") {
$json = readInputAsJSON();
foreach($annotations as $annotation){
if($annotation->id == $json->id){
$annotation->text = $json->text;
break;
}
}
saveAnnotations($annotations);
$output = ['status' => 'success'];
echo json_encode($output);
return;
}
elseif ($_GET['action'] == "delete") {
$json = readInputAsJSON();
$offset = 0;
foreach($annotations as $annotation){
if($annotation->id == $json->id){
break;
}
$offset++;
}
array_splice($annotations, $offset, 1);
saveAnnotations($annotations);
$output = ['status' => 'success'];
echo json_encode($output);
return;
}
function getAnnotations(){
if(file_exists(ANNOTATIONS_FILE) === false)
touch(ANNOTATIONS_FILE);
$text = file_get_contents(ANNOTATIONS_FILE);
$textjson = json_decode($text);
$annotations = [];
if($textjson)
$annotations = $textjson->annotations;
return $annotations;
}
function saveAnnotations($annotations){
if(file_exists(ANNOTATIONS_FILE) === false)
touch(ANNOTATIONS_FILE);
$data = ['annotations' => $annotations];
file_put_contents(ANNOTATIONS_FILE, json_encode($data));
}
function readInputAsJSON(){
$inputContent = '';
$input = fopen('php://input' , 'rb');
while (!feof($input)) {
$inputContent .= fread($input, 4096);
}
fclose($input);
return json_decode($inputContent);
}
// https://stackoverflow.com/a/40123420/10894055
function getGUID(){
if (function_exists('com_create_guid')){
return com_create_guid();
}
else {
mt_srand((double)microtime()*10000);
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>AnnotatorJS Demo</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="annotator-full.1.2.10/annotator-full.min.js"></script>
<link rel="stylesheet" href="annotator-full.1.2.10/annotator.min.css">
</head>
<body>
<div id="content">
<h1>Annotator Demo</h1>
<h3><a href="http://annotatorjs.org/">http://annotatorjs.org/</a></h3>
<ul>
<li>Download the raw PHP file <code>annotatorjs.php</code> and put it in a directory that is writeable (needs to be writeable as annotations saved to file <code>annotations.json</code>)</li>
<li>Download <a href="https://github.com/openannotation/annotator/releases/tag/v1.2.10">annotator-full.1.2.10.zip</a> and extract to folder <code>annotator-full.1.2.10</code></li>
<li>Run PHP server e.g. <code>php -S localhost:8080</code></li>
<li>Load this page on the server e.g. <code>http://localhost:8080/annotatorjs.php</code></li>
</ul>
<h4>Useful Javascript reference</h4>
<pre>
// initialise
var content = $('#content').annotator();
// set plugin store (needed for `dumpAnnotations()`)
content.annotator('addPlugin', 'Store', {
prefix: '',
urls: {
create: 'anot.php?action=create',
update: 'anot.php?action=update&id=:id',
destroy: 'anot.php?action=delete&id=',
search: 'anot.php?action=search'
},
annotationData: {
'uri': '<?php echo $_SERVER["PHP_SELF"] ?>'
},
loadFromSearch: {
'uri': '<?php echo $_SERVER["PHP_SELF"] ?>',
}
});
// save annotations
annotations = content.data('annotator').dumpAnnotations();
// destroy annotator
content.data('annotator').destroy();
content = $('#content').annotator();
// load annotations from file
content.data('annotator').loadAnnotations(annotations);
</pre>
<article class="showcase">
<div class="row">
<div class="col-sm-6">
<h2>Who's Using It?</h2>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://hypothes.is">Hypothes.is</a> is an open platform for the collaborative evaluation of knowledge. It combines sentence-level critique with community peer-review to provide commentary, references, and insight on top of news, blogs, scientific articles, books, terms of service, ballot initiatives, legislation and regulations, software code and more.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p>The <a href="http://openvideoannotation.org">Open Video Annotation Project</a> led by Harvard offers a simple way for anyone to create media-rich commentaries on fragments of video.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.edx.org">EdX</a> is a platform that hosts free courses designed for interactive study via the web, provided by MIT, Harvard and Berkeley.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://h2o.law.harvard.edu">H2O</a> is a suite of online classroom tools provided by Harvard’s Berkman Center that allow professors to freely develop, remix, and share online textbooks, casebooks, and modules under a Creative Commons license.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.annotationstudio.org">Annotation Studio</a> is a suite of web-based annotation tools developed by MIT’s HyperStudio that supports close reading and collaborative interpretation of online documents. It is designed primarily for humanities students and classrooms.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.writingpod.com">WritingPod</a> is an online writing community that allows users to share and get feedback on their writing, and engage with what they read.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.cbook.it/en">Crunched Book</a> (cbook) is a platform for reading and exploring literary works. It is based on semantic technologies and it is designed primarily for students and classrooms. Students and teachers can annotate portions of text, explore novel’s settings through interactive maps, add and share images and other contents related to a literary work, search for terms into the text (e.g. in the quoted speeches of a particular character), explore connections between characters in their social networks.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p>The <a href="https://it-dev.mpiwg-berlin.mpg.de/tracs/Annotations/wiki">Max Planck Institute for the History of Science</a> maintains a series of software projects for annotation of scanned books, images, and texts.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://austese.net">AustESE</a> is developing a set of interoperable services to support the publication of electronic scholarly editions by distributed collaborators in a Web 2.0 environment.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://oerpub.org">OERPUB</a> is creating open-source tools for authoring, adapting, remixing, and publishing open education resources and then delivering them to the web, mobile, tablet, and print.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://karmanotes.org">KarmaNotes</a> empowers college students to share course notes, study guides, and other digital learning resources online.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p>The OpenGov Foundation’s <a href="http://opengovfoundation.org/the-madison-project/">Madison Project</a> seeks to open government documents to allow for public discussion, collaboration and improvement.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://peerlibrary.org">PeerLibrary</a> provides a collaborative layer of knowledge on top of academic publications, allowing researchers to see and share real-time highlights and annotations.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.safaritutorials.com">Safari Tutorials</a> provides customized tutorials for training company employees.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://papergrader.org">Paper Grader</a> allows teachers and professors to quickly and efficiently collect, annotate, and grade student papers.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.siyavula.com">Siyavula</a> makes textbooks and resources available online for teachers and learners.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://open-apps.uoc.edu/">Universitat Oberta de Catalunya</a> maintains a set of open applications and teaching tools.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.InfiniteUlysses.com">Infinite Ulysses</a> supports public participation in the conversation around James Joyce's challenging novel by adding social voting and favoriting to Annotator.</p>
</div>
<div class="col-sm-6">
</div>
</div>
<div class="row">
<div class="col-sm-6">
<p><a href="http://www.lacunastories.com">Lacuna</a> is an open-source, online learning tool designed by the Stanford Poetic Media Lab to create new possibilities for reading and learning collaboratively. By focusing on tools for digital annotation, Lacuna allows instructors, students, and co-learners to discover different ways of reading, interpreting, and discussing course materials.</p>
</div>
<div class="col-sm-6">
</div>
</div>
</article>
</div>
<script type="text/javascript">
var content = $('#content').annotator();
content.annotator('addPlugin', 'Store', {
prefix: '',
urls: {
create: 'annotatorjs.php?action=create',
update: 'annotatorjs.php?action=update&id=:id',
destroy: 'annotatorjs.php?action=delete&id=',
search: 'annotatorjs.php?action=search'
},
annotationData: {
'uri': '<?php echo $_SERVER["PHP_SELF"] ?>'
},
loadFromSearch: {
'uri': '<?php echo $_SERVER["PHP_SELF"] ?>',
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment