diff --git a/test_input/content/img/bar.png b/test_input/content/img/bar.png new file mode 100644 index 0000000..b8c45bc Binary files /dev/null and b/test_input/content/img/bar.png differ diff --git a/test_input/content/img/foo.png b/test_input/content/img/foo.png new file mode 100644 index 0000000..3007543 Binary files /dev/null and b/test_input/content/img/foo.png differ diff --git a/test_input/content/img/more_img/banana.png b/test_input/content/img/more_img/banana.png new file mode 100644 index 0000000..631bdca Binary files /dev/null and b/test_input/content/img/more_img/banana.png differ diff --git a/test_input/testblog.py b/test_input/testblog.py index 3316839..524cf3c 100644 --- a/test_input/testblog.py +++ b/test_input/testblog.py @@ -5,15 +5,19 @@ sys.path.append('../') from verdandi.verdandi import Verdandi from verdandi.modules.page import Page +from verdandi.modules.commonassets import CommonAssets class TestPage1(Page): title = "A cool new Page" menu_title = "New Page" menu_label = "new_cool_page" - + class TestPage2(Page): title = "An other cool Page" + assets = [('img/foo.png', 'img/'), + ('img/foo.png', 'img/bar.png'), + ('img/foo.png', 'img/bar')] url = "page2.html" menu_title = "Other new Page" menu_label = "cool_page1" @@ -26,11 +30,15 @@ class TestPage3(Page): menu_parent = "cool_page1" url = "subdir/page3.html" +class Assets(CommonAssets): + assets = [('img', 'img/dir'), + ('img/', 'img/files')] class TestBlog(Verdandi): modules = [TestPage1(), TestPage2(), - TestPage3()] + TestPage3(), + Assets()] testblog = TestBlog() diff --git a/verdandi/mixins/assetsmixin.py b/verdandi/mixins/assetsmixin.py index 4d8c13c..a1064a9 100644 --- a/verdandi/mixins/assetsmixin.py +++ b/verdandi/mixins/assetsmixin.py @@ -1,11 +1,14 @@ #!/usr/bin/env python2 +import os +import shutil + from verdandi.constants import CONTENT_DIRECTORY class AssetsMixin(object): - + assets = [] - content_directory = CONTENT_DIRECTORY + asset_directory = CONTENT_DIRECTORY def get_assets(self): @@ -14,4 +17,51 @@ class AssetsMixin(object): def collect_assets(self, output_directory): assets = self.get_assets() - + for source, destination in assets: + source_path = os.path.join(self.asset_directory, source) + dest_path = os.path.join(output_directory, destination); + + if os.path.isdir(source_path): + self.copy_dir(source_path, dest_path) + elif os.path.isfile(source_path): + self.copy_file(source_path, dest_path) + else: + print "Skipping %s is neither directory nor file" % source_path + + + def copy_file(self, source_path, dest_path): + print "Copying %s to %s" % (source_path, dest_path) + + dest_dir = os.path.dirname(dest_path) + + if not os.path.exists(dest_dir): + os.makedirs(dest_dir) + shutil.copy(source_path, dest_path) + + + def copy_dir(self, source_path, dest_path): + # /foo/bar /rofl -> contents of bar go to rofl/bar + # /foo/bar/ /rofl -> contests of bar got to rofl/ + # Trailing slash on destination should have no effect + + # Will be '' in case of a trailing slash: /foo/bar/ else bar + source_base = os.path.basename(source_path) + # /rofl will become /rofl/ if base is '' otherwise it will become /rofl/bar + dest_path = os.path.join(dest_path, source_base) + + if not os.path.exists(dest_path): + os.makedirs(dest_path) + + # Discover the whole tree and copy each file individually + for source_dir, _, files in os.walk(source_path): + rel_path = os.path.relpath(source_dir, source_path) + # Purely cosmetical for debug output + if rel_path == '.': + dest_dir = dest_path + else: + dest_dir = os.path.join(dest_path, rel_path) + + for source_file in files: + file_source_path = os.path.join(source_dir, source_file) + file_dest_path = os.path.join(dest_dir, source_file) + self.copy_file(file_source_path, file_dest_path) diff --git a/verdandi/modules/commonassets.py b/verdandi/modules/commonassets.py new file mode 100644 index 0000000..36b9cad --- /dev/null +++ b/verdandi/modules/commonassets.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python2 + +from verdandi.mixins.assetsmixin import AssetsMixin +from verdandi.mixins.messagemixin import MessageMixin +from verdandi.mixins.rendermixin import RenderMixin + +class CommonAssets(AssetsMixin, MessageMixin, RenderMixin): + pass diff --git a/verdandi/modules/page.py b/verdandi/modules/page.py index df67ea8..a99e0a3 100644 --- a/verdandi/modules/page.py +++ b/verdandi/modules/page.py @@ -6,9 +6,10 @@ import markdown from verdandi.mixins.templatemixin import TemplateMixin from verdandi.mixins.menuitemmixin import MenuItemMixin +from verdandi.mixins.assetsmixin import AssetsMixin from verdandi.constants import CONTENT_DIRECTORY -class Page(MenuItemMixin, TemplateMixin): +class Page(MenuItemMixin, TemplateMixin, AssetsMixin): title = "Page Title" content_file = "content.md"