Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# MLA Citation Machine

This project provides a small command line program that generates a basic MLA style citation for a web page.

## Usage

Run the application and provide a URL through standard input. The program will download the page, attempt to detect author, title, website name and publication date from common HTML meta tags, and print a citation string.

```
$ echo "https://example.com" | ./gradlew run
```

The output will resemble:

```
Author Name. "Article Title." Website Name, 2024-03-14, https://example.com
```

The extraction heuristics are simple and may not work for every site but should handle many common news pages.
Empty file modified gradlew
100644 → 100755
Empty file.
79 changes: 79 additions & 0 deletions src/main/java/CitationGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.net.URL;

public class CitationGenerator {
public static String generateMlaCitation(String url) throws Exception {
Document doc = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36")
.referrer("http://www.google.com")
.header("Accept-Language", "en-US,en;q=0.9")
.timeout(10 * 1000)
.followRedirects(true)
.get();

String author = null;
String[] authorSelectors = {
"meta[name=author]",
"meta[property*=author]",
"meta[name*=author]"
};
for (String selector : authorSelectors) {
Element element = doc.selectFirst(selector);
if (element != null) {
String content = element.attr("content");
if (!content.isEmpty()) {
author = content;
break;
}
}
}

String title = null;
Element titleMeta = doc.selectFirst("meta[property=og:title]");
if (titleMeta != null && !titleMeta.attr("content").isEmpty()) {
title = titleMeta.attr("content");
} else {
title = doc.title();
}

String website = null;
Element siteMeta = doc.selectFirst("meta[property=og:site_name]");
if (siteMeta != null && !siteMeta.attr("content").isEmpty()) {
website = siteMeta.attr("content");
} else {
website = new URL(url).getHost();
}

String date = null;
String[] dateSelectors = {
"meta[property*=published]",
"meta[name*=pubdate]",
"meta[name*=date]"
};
for (String selector : dateSelectors) {
Element element = doc.selectFirst(selector);
if (element != null) {
String content = element.attr("content");
if (!content.isEmpty()) {
date = content;
break;
}
}
}

StringBuilder citation = new StringBuilder();
if (author != null) {
citation.append(author).append(". ");
}
citation.append("\"").append(title).append(".\" ");
citation.append(website);
if (date != null) {
citation.append(", ").append(date);
}
citation.append(", ").append(url);
return citation.toString();
}
}
25 changes: 5 additions & 20 deletions src/main/java/mainMla.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.Scanner;

public class mainMla {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
try{
Document doc = Jsoup.connect(input)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0")
.referrer("http://www.google.com")
.header("Accept-Language", "en-US,en;q=0.9")
.timeout(10*1000)
.followRedirects(true)
.get();
System.out.println(doc.getElementsByTag("meta"));
Element authorData = doc.selectFirst("meta[property=cXenseParse:author]");
System.out.println(authorData.attr("content"));



}
catch(Exception e){
try {
String citation = CitationGenerator.generateMlaCitation(input);
System.out.println(citation);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
Expand Down