Skip to content

Commit

Permalink
Add website to view links.
Browse files Browse the repository at this point in the history
Go to localhost:4567 while it's running to view the website.
  • Loading branch information
avp committed Aug 4, 2013
1 parent a00fa42 commit 90124cf
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 10 deletions.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-freemarker</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
49 changes: 39 additions & 10 deletions src/main/java/com/medee/wikigraph/Main.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
package com.medee.wikigraph;

/** This is the main class. */
import org.json.JSONArray;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.template.freemarker.FreeMarkerRoute;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static spark.Spark.get;
import static spark.Spark.staticFileLocation;

/**
* This is the main class.
*/
public class Main {

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Specify a single article");
return;
}
String wikiPage = args[0];
WikipediaReader wikipediaReader = new WikipediaReader();
for (String link : wikipediaReader.linksOnArticle(wikiPage)) {
System.out.println(link);
}
staticFileLocation("static");

get(new FreeMarkerRoute("/") {
@Override
public Object handle(Request request, Response response) {
Map<String, Object> templateValues = new HashMap<String, Object>();

return modelAndView(templateValues, "index.ftl");
}
});

get(new Route("/page") {
@Override
public Object handle(Request request, Response response) {
String wikiPage = (String) request.queryParams("page");
WikipediaReader wikipediaReader = new WikipediaReader();
List<String> links = wikipediaReader.linksOnArticle(wikiPage);

JSONArray linksJson = new JSONArray(links);

return linksJson.toString();
}
});

}
}
36 changes: 36 additions & 0 deletions src/main/resources/spark/template/freemarker/index.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Wiki Graph</title>

<link rel="stylesheet" href="/css/bootstrap.min.css">
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">

<link rel="stylesheet" href="/css/index.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Wiki Graph</h1>
<div class="input-group">
<input id="input-page" class="form-control input-large" type="text" placeholder="Page to query">
<span class="input-group-btn">
<button id="btn-refresh" class="btn btn-primary btn-large">Refresh</button>
</span>
</div>
</div>

<div class="panel" id="panel-links">
<div class="panel-heading">
<h2>Links on page - <span id="count-links">0</span> links found</h2>
</div>
<div id="links">
<h3>Run a query to see the links.</h3>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/index.js"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions src/main/resources/static/css/bootstrap.min.css

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/main/resources/static/css/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: #aaa;
}
6 changes: 6 additions & 0 deletions src/main/resources/static/js/bootstrap.min.js

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions src/main/resources/static/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var WikiGraph = {};

WikiGraph.getDataForPage = function(page) {
$('#links').html('<i class="icon-spinner icon-spin icon-4x"></i>');
$.ajax('/page', {
method: 'get',
data: {page: page},
success: function(data) {
data = JSON.parse(data);
result = data.join('<br>');
$('#links').html(result);
$('#count-links').html(data.length);
},
error: function() {
$('#links').html('<h3>That page doesn\'t exist.</h3>');
$('#count-links').html('0');
}
});
};

WikiGraph.init = function() {
$('#btn-refresh').click(function() {
WikiGraph.getDataForPage($('#input-page').val());
});

$('#input-page').keypress(function(e) {
if (e.which === 13) {
e.preventDefault();
$('#btn-refresh').click();
}
});
};

WikiGraph.init();

0 comments on commit 90124cf

Please sign in to comment.