From b071da73c15c165537303e948d749a91080afd11 Mon Sep 17 00:00:00 2001 From: Eric Huang <121286342+erichuang0324@users.noreply.github.com> Date: Mon, 9 Jun 2025 16:59:43 +0800 Subject: [PATCH] Add simple MLA citation generator --- README.md | 19 +++++++ gradlew | 0 src/main/java/CitationGenerator.java | 79 ++++++++++++++++++++++++++++ src/main/java/mainMla.java | 25 ++------- 4 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 README.md mode change 100644 => 100755 gradlew create mode 100644 src/main/java/CitationGenerator.java diff --git a/README.md b/README.md new file mode 100644 index 0000000..18b6a15 --- /dev/null +++ b/README.md @@ -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. diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/CitationGenerator.java b/src/main/java/CitationGenerator.java new file mode 100644 index 0000000..b94b4f0 --- /dev/null +++ b/src/main/java/CitationGenerator.java @@ -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(); + } +} diff --git a/src/main/java/mainMla.java b/src/main/java/mainMla.java index 385d25b..31ebcdc 100644 --- a/src/main/java/mainMla.java +++ b/src/main/java/mainMla.java @@ -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()); } }