-
Notifications
You must be signed in to change notification settings - Fork 0
/
publishtarget.py
56 lines (44 loc) · 2.02 KB
/
publishtarget.py
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
import os
import shutil
class PublishTarget:
"""
A class representing a target directory for publishing to.
"""
ATTACHMENTS_DIR = "attachments" # sits in publish_directory
def __init__(self, publish_directory, publish_files, publish_attachments):
self.publish_directory = publish_directory
self.publish_target = {}
self.publish_files = publish_files
self.attachments = publish_attachments
def define_publish_target(self, publish_target):
self.publish_target = publish_target
def create_publish_structure(self):
if not os.path.exists(self.publish_directory):
os.mkdir(self.publish_directory)
if not os.path.exists(os.path.join(self.publish_directory, self.ATTACHMENTS_DIR)):
os.mkdir(os.path.join(self.publish_directory, self.ATTACHMENTS_DIR))
for subfolder in self.publish_target.keys():
dir_to_create = os.path.join(self.publish_directory, subfolder)
if not os.path.exists(dir_to_create):
os.mkdir(dir_to_create)
def write_markdown(self):
for subfolder, md in self.publish_target.items():
f = open(os.path.join(self.publish_directory, subfolder, "index.md"), "w")
f.write(md)
def build_publish_dict(self):
publish_dict = {}
for pub_file in self.publish_files:
pub_file.convert_local_refs()
publish_dict[pub_file.get_publish_name()] = pub_file.dump_markdown()
return publish_dict
def publish_markdown(self):
# def publish_markdown(self, pub_path: str):
publish_dict = self.build_publish_dict()
self.define_publish_target(publish_dict)
self.create_publish_structure()
self.write_markdown()
def publish_attachments(self):
for attachment in self.attachments:
source_path = attachment.full_path
publish_path = os.path.join(self.publish_directory, self.ATTACHMENTS_DIR, attachment.filename)
shutil.copy(source_path, publish_path)