|
1 | | -# Find the latest open publications issue |
| 1 | +# Find all open publications issues |
2 | 2 | open_issues <- gh::gh( |
3 | 3 | "/repos/{gh_repo}/issues", |
4 | 4 | gh_repo = gh_repository, |
5 | 5 | state = "open", |
6 | 6 | labels = "publications", |
7 | 7 | sort = "created", |
8 | | - direction = "desc", |
9 | | - per_page = 1 |
| 8 | + direction = "asc", |
| 9 | + per_page = 100 |
10 | 10 | ) |
11 | 11 |
|
12 | 12 | if (length(open_issues) == 0) { |
13 | | - message("No open publications issue found") |
| 13 | + message("No open publications issues found") |
14 | 14 | quit(save = "no") |
15 | 15 | } |
16 | 16 |
|
17 | | -issue <- open_issues[[1]] |
18 | | -issue_number <- issue$number |
| 17 | +message(sprintf("Found %d open publications issue(s)", length(open_issues))) |
19 | 18 |
|
20 | | -last_comment_pubs <- issue$body_html |
21 | | -if (is.null(last_comment_pubs)) { |
22 | | - # Fetch with HTML body |
23 | | - issue <- gh::gh( |
24 | | - "/repos/{gh_repo}/issues/{issue_number}", |
25 | | - gh_repo = gh_repository, |
26 | | - issue_number = issue_number, |
27 | | - .accept = "application/vnd.github.v3.full+json" |
28 | | - ) |
29 | | - last_comment_pubs <- issue$body_html |
30 | | -} |
31 | | - |
32 | | -last_comment_pubs <- xml2::read_html(last_comment_pubs) |
33 | | - |
34 | | -# Extract BibTeX from the code block inside <details> |
35 | | -# GitHub renders markdown code fences as <pre><code> elements |
36 | | -bib_text <- last_comment_pubs |> |
37 | | - xml2::xml_find_first("//details//pre") |> |
38 | | - xml2::xml_text() |
| 19 | +# Process each issue and collect selected papers |
| 20 | +all_selected_bibs <- list() |
39 | 21 |
|
40 | | -# If no <pre> found, try the <details> element directly (fallback) |
41 | | -if (is.na(bib_text) || nchar(trimws(bib_text)) == 0) { |
42 | | - bib_text <- last_comment_pubs |> |
43 | | - xml2::xml_find_first("//details") |> |
44 | | - xml2::xml_text() |
45 | | -} |
46 | | - |
47 | | -# Clean up any leading/trailing whitespace |
48 | | -bib_text <- trimws(bib_text) |
49 | | - |
50 | | -if (is.na(bib_text) || nchar(bib_text) == 0) { |
51 | | - message("No BibTeX content found in issue") |
52 | | - quit(save = "no") |
53 | | -} |
| 22 | +for (issue in open_issues) { |
| 23 | + issue_number <- issue$number |
| 24 | + message(sprintf("Processing issue #%d", issue_number)) |
54 | 25 |
|
55 | | -write(bib_text, "bibentries_previous_month.bib") |
| 26 | + # Get HTML body |
| 27 | + issue_html <- issue$body_html |
| 28 | + if (is.null(issue_html)) { |
| 29 | + issue <- gh::gh( |
| 30 | + "/repos/{gh_repo}/issues/{issue_number}", |
| 31 | + gh_repo = gh_repository, |
| 32 | + issue_number = issue_number, |
| 33 | + .accept = "application/vnd.github.v3.full+json" |
| 34 | + ) |
| 35 | + issue_html <- issue$body_html |
| 36 | + } |
56 | 37 |
|
57 | | -bibentries_previous_month <- bibtex::read.bib("bibentries_previous_month.bib") |
| 38 | + parsed_html <- xml2::read_html(issue_html) |
58 | 39 |
|
59 | | -selected <- last_comment_pubs |> |
60 | | - xml2::xml_find_first("//ul") |> |
61 | | - xml2::xml_find_all(".//input") |> |
62 | | - xml2::xml_has_attr("checked") |
| 40 | + # Extract BibTeX from the code block inside <details> |
| 41 | + bib_text <- parsed_html |> |
| 42 | + xml2::xml_find_first("//details//pre") |> |
| 43 | + xml2::xml_text() |
63 | 44 |
|
64 | | -if (!any(selected)) { |
65 | | - message("No papers selected, closing issue") |
| 45 | + # Fallback to <details> directly |
| 46 | + if (is.na(bib_text) || nchar(trimws(bib_text)) == 0) { |
| 47 | + bib_text <- parsed_html |> |
| 48 | + xml2::xml_find_first("//details") |> |
| 49 | + xml2::xml_text() |
| 50 | + } |
| 51 | + |
| 52 | + bib_text <- trimws(bib_text) |
| 53 | + |
| 54 | + if (is.na(bib_text) || nchar(bib_text) == 0) { |
| 55 | + message(sprintf(" No BibTeX content found in issue #%d, closing", issue_number)) |
| 56 | + gh::gh( |
| 57 | + "PATCH /repos/{gh_repo}/issues/{issue_number}", |
| 58 | + gh_repo = gh_repository, |
| 59 | + issue_number = issue_number, |
| 60 | + state = "closed", |
| 61 | + labels = list("publications", "processed") |
| 62 | + ) |
| 63 | + next |
| 64 | + } |
| 65 | + |
| 66 | + # Write and read bib entries |
| 67 | + write(bib_text, "bibentries_temp.bib") |
| 68 | + bibentries <- bibtex::read.bib("bibentries_temp.bib") |
| 69 | + |
| 70 | + # Find selected (checked) papers |
| 71 | + selected <- parsed_html |> |
| 72 | + xml2::xml_find_first("//ul") |> |
| 73 | + xml2::xml_find_all(".//input") |> |
| 74 | + xml2::xml_has_attr("checked") |
| 75 | + |
| 76 | + if (!any(selected)) { |
| 77 | + message(sprintf(" No papers selected in issue #%d, closing", issue_number)) |
| 78 | + } else { |
| 79 | + selected_bibentries <- bibentries[selected] |
| 80 | + message(sprintf(" Found %d selected paper(s)", length(selected_bibentries))) |
| 81 | + all_selected_bibs <- c(all_selected_bibs, selected_bibentries) |
| 82 | + } |
| 83 | + |
| 84 | + # Close the issue and add "processed" label |
66 | 85 | gh::gh( |
67 | 86 | "PATCH /repos/{gh_repo}/issues/{issue_number}", |
68 | 87 | gh_repo = gh_repository, |
69 | 88 | issue_number = issue_number, |
70 | 89 | state = "closed", |
71 | 90 | labels = list("publications", "processed") |
72 | 91 | ) |
73 | | - quit(save = "no") |
74 | 92 | } |
75 | 93 |
|
76 | | -selected_bibentries <- bibentries_previous_month[selected] |
| 94 | +# Clean up temp file |
| 95 | +unlink("bibentries_temp.bib") |
| 96 | + |
| 97 | +if (length(all_selected_bibs) == 0) { |
| 98 | + message("No papers selected across all issues") |
| 99 | + quit(save = "no") |
| 100 | +} |
77 | 101 |
|
78 | | -bib_new <- vapply(selected_bibentries, format, style = "Bibtex", character(1)) |
| 102 | +message(sprintf("Adding %d paper(s) to papers.bib", length(all_selected_bibs))) |
79 | 103 |
|
| 104 | +# Add all selected papers to bib file |
| 105 | +bib_new <- vapply(all_selected_bibs, format, style = "Bibtex", character(1)) |
80 | 106 | bib_old <- readLines("_data/papers.bib") |
81 | | - |
82 | 107 | bib <- paste(c(bib_old, bib_new), collapse = "\n") |
83 | | - |
84 | 108 | write(bib, "_data/papers.bib") |
85 | 109 |
|
86 | 110 | ## filter latest version |
@@ -120,12 +144,3 @@ df <- df |> |
120 | 144 | dplyr::select(-n, -id, -n_pp) |
121 | 145 |
|
122 | 146 | bib2df::df2bib(df, file = "_data/papers.bib") |
123 | | - |
124 | | -# Close the issue and add "processed" label |
125 | | -gh::gh( |
126 | | - "PATCH /repos/{gh_repo}/issues/{issue_number}", |
127 | | - gh_repo = gh_repository, |
128 | | - issue_number = issue_number, |
129 | | - state = "closed", |
130 | | - labels = list("publications", "processed") |
131 | | -) |
|
0 commit comments