Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
[feature][WDT-116] Data provider for sphinx html



See merge request !1
  • Loading branch information
ikeeip committed May 4, 2016
2 parents a8d2d8d + dc98aa9 commit a77766e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
42 changes: 42 additions & 0 deletions conf_publisher/data_providers/sphinx_base_data_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os

from . import DataProvider


class SphinxBaseDataProvider(DataProvider):

DEFAULT_SOURCE_EXT = None
DEFAULT_ROOT_DIR = './'
DEFAULT_SOURCE_DIR = 'docs/build/result'
DEFAULT_IMAGES_DIR = '_images'
DEFAULT_DOWNLOADS_DIR = '_downloads'

def __init__(self, root_dir=None, base_dir=None, downloads_dir=None, images_dir=None, source_ext=None):
self._root_dir = os.path.abspath(root_dir or './')

self._source_dir = base_dir or self.DEFAULT_SOURCE_DIR
if not os.path.isabs(self._source_dir):
self._source_dir = os.path.join(self._root_dir, self._source_dir)

self._downloads_dir = downloads_dir or self.DEFAULT_DOWNLOADS_DIR
if not os.path.isabs(self._downloads_dir):
self._downloads_dir = os.path.join(self._source_dir, self._downloads_dir)

self._images_dir = images_dir or self.DEFAULT_IMAGES_DIR
if not os.path.isabs(self._images_dir):
self._images_dir = os.path.join(self._source_dir, self._images_dir)

self._source_ext = source_ext or self.DEFAULT_SOURCE_EXT

def get_source(self, filename):
print self._source_dir, self._source_ext
return os.path.join(self._source_dir, filename + self._source_ext)

def get_source_data(self, filename):
raise NotImplementedError()

def get_image(self, filename):
return os.path.join(self._images_dir, filename)

def get_attachment(self, filename):
return os.path.join(self._downloads_dir, filename)
38 changes: 9 additions & 29 deletions conf_publisher/data_providers/sphinx_fjson_data_provider.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
import os

from . import DataProvider
from .sphinx_base_data_provider import SphinxBaseDataProvider
from ..serializers import json_serializer


class SphinxFJsonDataProvider(DataProvider):
def __init__(self, root_dir=None, base_dir=None, downloads_dir=None, images_dir=None, source_ext=None):
self._root_dir = os.path.abspath(root_dir or './')

self._source_dir = base_dir or 'docs/build/json'
if not os.path.isabs(self._source_dir):
self._source_dir = os.path.join(self._root_dir, self._source_dir)

self._downloads_dir = downloads_dir or '_downloads'
if not os.path.isabs(self._downloads_dir):
self._downloads_dir = os.path.join(self._source_dir, self._downloads_dir)

self._images_dir = images_dir or '_images'
if not os.path.isabs(self._images_dir):
self._images_dir = os.path.join(self._source_dir, self._images_dir)

self._source_ext = source_ext or '.fjson'

def get_source(self, filename):
return os.path.join(self._source_dir, filename + self._source_ext)
class SphinxFJsonDataProvider(SphinxBaseDataProvider):
DEFAULT_SOURCE_EXT = '.fjson'
DEFAULT_SOURCE_DIR = 'docs/build/json'

def get_source_data(self, filename):
with open(filename, 'r') as f:
if os.path.isabs(filename):
filepath = filename
else:
filepath = self.get_source(filename)
with open(filepath, 'r') as f:
content = json_serializer.load(f)

return content['title'], content['body']

def get_image(self, filename):
return os.path.join(self._images_dir, filename)

def get_attachment(self, filename):
return os.path.join(self._downloads_dir, filename)
16 changes: 16 additions & 0 deletions conf_publisher/data_providers/sphinx_html_data_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .sphinx_base_data_provider import SphinxBaseDataProvider
import re
import codecs


class SphinxHTMLDataProvider(SphinxBaseDataProvider):
DEFAULT_SOURCE_EXT = '.html'
DEFAULT_SOURCE_DIR = 'docs/build/html'

def get_source_data(self, filename):
with codecs.open(filename, 'r', encoding='utf-8') as f:
content = f.read()
title = re.findall(r'<title.*?>(.+?)</title>', content)[0]
body = re.findall(r'<body.*?>(.+?)</body>', content, re.DOTALL)[0].strip()

return title, body
21 changes: 18 additions & 3 deletions conf_publisher/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@
from .config import ConfigLoader, flatten_page_config_list, PageImageAattachmentConfig
from .constants import DEFAULT_CONFLUENCE_API_VERSION, DEFAULT_WATERMARK_CONTENT
from .data_providers.sphinx_fjson_data_provider import SphinxFJsonDataProvider
from .data_providers.sphinx_html_data_provider import SphinxHTMLDataProvider
from .mutators.page_mutator import WatermarkPageMutator, LinkPageMutator


def get_data_provider_class(config):
if config.source_ext == '.html':
data_provider_class = SphinxHTMLDataProvider
else:
data_provider_class = SphinxFJsonDataProvider
return data_provider_class


def create_publisher(config, confluence_api):
page_manager = ConfluencePageManager(confluence_api)
attachment_publisher = AttachmentPublisher(confluence_api)
data_provider = SphinxFJsonDataProvider(base_dir=config.base_dir, downloads_dir=config.downloads_dir,
images_dir=config.images_dir, source_ext=config.source_ext)

data_provider_class = get_data_provider_class(config)

data_provider = data_provider_class(
base_dir=config.base_dir,
downloads_dir=config.downloads_dir,
images_dir=config.images_dir,
source_ext=config.source_ext
)
return Publisher(config, data_provider, page_manager, attachment_publisher)


Expand All @@ -32,7 +47,7 @@ def _page_title(current_title, new_title, config_title=None, hold_current=False)
return current_title
if config_title:
return config_title
return new_title
return new_title if new_title else current_title

def _page(self, current_page, source):
page = copy.copy(current_page)
Expand Down

0 comments on commit a77766e

Please sign in to comment.