-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwp_import.py
More file actions
333 lines (255 loc) · 11.1 KB
/
wp_import.py
File metadata and controls
333 lines (255 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import requests
import urllib
from bs4 import BeautifulSoup
import pypandoc
import re
import link_citation_converter
def convert_post_json(post_json, args):
post_data = {}
json = post_json
post_data["title"] = json["title"]["rendered"]
if json["date"]:
post_data["date"] = json["date"]
post_data["link"] = json["link"]
if json["acf"]["subheadline"]:
post_data["subtitle"] = json["acf"]["subheadline"]
if json["acf"]["doi"]:
post_data["doi"] = json["acf"]["doi"]
raw_content = json["content"]["rendered"]
soup = BeautifulSoup(raw_content, 'html.parser')
footnotes_dict = []
if args.with_footnotes:
footnotes = soup.find_all("span", class_="footnote_referrer")
for footnote in footnotes:
footnote_id = footnote.find("sup", class_="footnote_plugin_tooltip_text").get("id").replace("tooltip",
"reference")
footnote_text = soup.find("a", id=footnote_id).find_parent("tr").find("td",
class_="footnote_plugin_text").get_text()
footnotes_dict.append(footnote_text)
footnote.replace_with("wp2latex-footnote-placeholder-" + str(len(footnotes_dict) - 1))
frc = soup.find("div", class_="footnotes_reference_container")
if frc:
frc.decompose()
raw_html = soup.__str__()
result = pypandoc.convert_text(raw_html, 'tex', format='html', extra_args=["--shift-heading-level-by=-1"])
# TODO: convert footnote text to latex to convert links etc.
if args.with_footnotes:
for footnote_count in range(len(footnotes_dict) - 1, -1, -1):
command_start = ""
if args.endnotes:
command_start = "\\endnote{"
else:
command_start = "\\footnote{"
result = result.replace("wp2latex-footnote-placeholder-" + str(footnote_count),
command_start + footnotes_dict[footnote_count] + "}")
first_letter = result[0]
if args.first_letter_before:
first_letter = args.first_letter_before + first_letter
if args.first_letter_after:
first_letter += args.first_letter_after
if args.first_letter_after or args.first_letter_before:
result = first_letter + " " + result[1:]
if args.fix_sections:
result = fix_sections(result)
if args.convert_links_to_citations:
result = link_citation_converter.convert_links_to_citations(result, args)
if args.remove_ulines:
result = result.replace("\\uline", "")
result = result.replace("\\ul", "")
result = re.sub(r"(?<!\n)\n(?!\n)", " ", result)
post_data["content"] = result
return post_data
def import_post(host, slug, args):
print("Slug for post is " + slug + ", host is " + host + ". Trying to get post from API.")
post = requests.get("https://" + host + "/wp-json/wp/v2/posts?slug=" + slug)
if post.status_code != 200:
print("Couldn't get post from API!")
return
return convert_post_json(post.json()[0], args)
def generate_post(post_data, args):
post_template = ""
if args.single_post_template:
template_path = args.single_post_template
else:
template_path = "latex-templates/single_post.tex"
with open(template_path, 'r', encoding="utf-8") as f:
post_template = f.read()
authors_string = ""
#for author in post_data["authors"]:
# authors_string += author + ", "
#authors_string = authors_string[:-2]
result = post_template.replace("[wp2latex-post-title]", tex_escape(post_data["title"])) \
.replace("[wp2latex-post-authors]", tex_escape(authors_string)) \
.replace("[wp2latex-post-url]", tex_escape(post_data["link"])) \
.replace("[wp2latex-post-content]", post_data["content"])
if "subtitle" in post_data:
result = result.replace("[wp2latex-post-subtitle]", tex_escape(post_data["subtitle"]))
else:
result = result.replace("[wp2latex-post-subtitle]", "")
if "doi" in post_data:
result = result.replace("[wp2latex-post-doi]", tex_escape(post_data["doi"]))
else:
result = result.replace("[wp2latex-post-doi]", "")
# Add \printendnotes if endnotes activated and there is at least one endnote
if args.endnotes and (result.find("\\endnote") != -1 or result.find(args.cite_command) != -1):
result = result.replace("[wp2latex-print-endnotes-if-any]", "\\pagebreak\\printendnotes")
else:
result = result.replace("[wp2latex-print-endnotes-if-any]", "")
return result
def get_host_slug_from_url(url):
parsed_url = urllib.parse.urlparse(url)
host = parsed_url.netloc
slug = parsed_url.path[1:-1]
return host, slug
def check_if_url_is_category(url):
parsed_url = urllib.parse.urlparse(url)
if parsed_url.path.startswith("/category/"):
return True
else:
return False
def get_category_host_slug_from_url(url):
parsed_url = urllib.parse.urlparse(url)
host = parsed_url.netloc
raw_slug = parsed_url.path[:-1]
slug = raw_slug.split("/")[-1]
return host, slug
def get_category_id_from_slug(host, slug):
category = requests.get("https://" + host + "/wp-json/wp/v2/categories?slug=" + slug)
if category.status_code != 200:
print("Couldn't get category id for URL from API!")
return
return category.json()[0]["id"]
def get_all_posts(host, categories, exclude_categories, after, before, recursive_exclude_categories=False, page=1):
global parent_category
query = "https://" + host + "/wp-json/wp/v2/posts"
first_argument = True
if categories:
query += "?categories=" + str(categories)
first_argument = False
if exclude_categories:
if first_argument:
query += "?"
first_argument = False
else:
query += "&"
query += "categories_exclude=" + str(exclude_categories)
if before:
if first_argument:
query += "?"
first_argument = False
else:
query += "&"
query += "before=" + str(before)
if after:
if first_argument:
query += "?"
first_argument = False
else:
query += "&"
query += "after=" + str(after)
if first_argument:
query += "?per_page=100&page=" + str(page)
else:
query += "&per_page=100&page=" + str(page)
print("Doing request to API target " + query)
raw_posts = requests.get(query)
if raw_posts.status_code != 200:
print("Couldn't get posts from API: " + raw_posts.text)
return
total_pages = int(raw_posts.headers.get("X-WP-TotalPages"))
posts = raw_posts.json()
if page < total_pages:
posts += get_all_posts(host, categories, after, before, page + 1, recursive_exclude_categories)
print("Got " + str(len(posts)) + " posts.")
if recursive_exclude_categories:
new_posts = []
for post in posts:
categories = post["categories"]
excluded_category_found = False
for category in categories:
print("Getting parent categories for post " + post["slug"])
parent_categories = get_parent_categories(host, category)
print("parent categories:" + str(parent_categories))
for parent_category in parent_categories:
print("Test:" + str(exclude_categories.split(",")))
if str(parent_category) in exclude_categories.split(","):
print("Found post where parent category is excluded category, removing " + post["slug"])
excluded_category_found = True
break
if excluded_category_found:
break
if not excluded_category_found:
new_posts.append(post)
posts = new_posts
return posts
def download_post(host, slug, args, posts_directory, post_count):
print("Downloading post " + slug)
post_in_latex = generate_post(import_post(host, slug, args), args)
with open(posts_directory + "/post_" + str(post_count) + ".tex", "x", encoding="utf-8") as f:
f.write(post_in_latex)
def fix_sections(input_str):
regex = r"\\hypertarget\{[^}]+\}\{%\n\\section{\\texorpdfstring{\\textbf\{[^}]+\}}\{[^}]+\}\}\\label\{[^}]+\}}"
regex2 = r"\\hypertarget\{[^}]+\}\{%\n\\section{[^}]+\}\\label\{[^}]+\}}"
matches = re.findall(regex, input_str)
matches2 = re.findall(regex2, input_str)
for match in matches:
old_section_command = match
regex2 = r"\\section{\\texorpdfstring{\\textbf{([^}]+)}}{[^}]+}}\\label{[^}]+}"
section_title = re.search(regex2, old_section_command).group(1)
input_str = input_str.replace(old_section_command, "\\section*{" + tex_escape(section_title) + "}")
for match in matches2:
old_section_command = match
regex2 = r"\\section{([^{}]+)"
section_title = re.search(regex2, old_section_command).group(1)
input_str = input_str.replace(old_section_command, "\\section*{" + tex_escape(section_title) + "}")
input_str = input_str.replace("\\section{", "\\section*{")
return input_str
def get_parent_categories(host, category_id):
category = requests.get("https://" + host + "/wp-json/wp/v2/categories/" + str(category_id))
print("Requesting category " + "https://" + host + "/wp-json/wp/v2/categories/" + str(category_id))
if category.status_code != 200 or len(category.json()) == 0:
print("Couldn't get category id for URL from API!")
return []
category = category.json()
parent_categories = []
if category["parent"] != 0:
parent_categories.append(category["parent"])
print("Found parent category:" + str(category["parent"]) + ", current categories: " + str(parent_categories))
parent_categories += get_parent_categories(host, category["parent"])
else:
print("Category " + str(category_id) + " has no parents. Returning " + str(parent_categories))
return parent_categories
def tex_escape(text):
conv = {
'&': r'\&',
'%': r'\%',
'$': r'\$',
'#': r'\#',
'_': r'\_',
'{': r'\{',
'}': r'\}',
'~': r'\textasciitilde{}',
'^': r'\^{}',
'\\': r'\textbackslash{}',
'<': r'\textless{}',
'>': r'\textgreater{}',
}
regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key=lambda item: - len(item))))
return regex.sub(lambda match: conv[match.group()], text)
def tex_unescape(text):
conv = {
'\&': r'&',
'\%': r'%',
'\$': r'$',
'\#': r'#',
'\_': r'_',
'\{': r'{',
'\}': r'}',
'\\textasciitilde{}': r'~',
'\^{}': r'^',
'\\textbackslash{}': r'\\',
'\\textless{}': r'<',
'\\textgreater{}': r'>',
}
regex = re.compile('|'.join(re.escape(str(key)) for key in sorted(conv.keys(), key=lambda item: - len(item))))
return regex.sub(lambda match: conv[match.group()], text)