Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeeip committed Jun 7, 2016
2 parents a77766e + f857738 commit 249321c
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 99 deletions.
122 changes: 61 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Confluence Publisher #
# Confluence Publisher

[![Build Status](https://travis-ci.org/Arello-Mobile/confluence-publisher.svg?branch=master)](https://travis-ci.org/Arello-Mobile/confluence-publisher)

Expand All @@ -10,14 +10,68 @@ Set of tools to help publish documentation to Confluence. It includes:
This tools use own configuration file.

For now it supports:
- confluence versions: 5.5
- sphinx-build formats: "json", "json_conf"
- confluence versions: 5.5 - 5.9
- sphinx-build formats: "fjson", "html"

## Installation ##
## Installation

> pip install confluence-publisher

## Configuration file format ##
## Publisher

> conf_publisher config.yml --auth XXXXXjpwYXNzdXXXXX==

If a config doesn't contain page.id, you can use ``conf_page_maker`` command
to create a page and page ID will be put into config automatically.

```
usage: conf_publisher [-h] [-u URL] [-a AUTH] [-F] [-w WATERMARK] [-l LINK]
[-ht] [-v]
config
Publish documentation (Sphinx fjson) to Confluence
positional arguments:
config Configuration file
optional arguments:
-h, --help show this help message and exit
-u URL, --url URL Confluence Url
-a AUTH, --auth AUTH Base64 encoded user:password string
-F, --force Publish not changed page.
-w WATERMARK, --watermark WATERMARK
Overrides the watermarks. Also can be "False" to
remove all watermarks; or "True" to add watermarkswith
default text: "Automatically generated content. Do not
edit directly." on all pages.
-l LINK, --link LINK Overrides page link. If value is "False" then removes
the link.
-ht, --hold-titles Do not change page titles while publishing.
-v, --verbose
```

## Page Maker

> conf_page_maker config.yml --auth XXXXXjpwYXNzdXXXXX== --parent-id 52332132

```
usage: conf_page_maker [-h] [-u URL] [-a AUTH] [-pid PARENT_ID] [-v] config
Create Confluence pages and update configuration file with it ids
positional arguments:
config Configuration file
optional arguments:
-h, --help show this help message and exit
-u URL, --url URL Confluence Url
-a AUTH, --auth AUTH Base64 encoded user:password string
-pid PARENT_ID, --parent-id PARENT_ID
Parent page ID in confluence.
-v, --verbose
```

## Configuration file format

Directives:

Expand All @@ -39,15 +93,15 @@ Directives:
- **images**
- path_to_img1
- path_to_img2
- **downloads**
- **downloads**
- path_to_file1
- path_to_file2
- **pages** Subpages to be published.

- **...** same structure as for pages


### Config example ###
### Config example

```
version: 2
Expand Down Expand Up @@ -105,57 +159,3 @@ or more JSONify style:
]
}
```

## Publisher ##

> conf_publisher config.yml --auth XXXXXjpwYXNzdXXXXX==

If a config doesn't contain page.id, you can use ``conf_page_maker`` command
to create a page and page ID will be put into config automatically.

```
usage: conf_publisher [-h] [-u URL] [-a AUTH] [-F] [-w WATERMARK] [-l LINK]
[-ht] [-v]
config
Publish documentation (Sphinx fjson) to Confluence
positional arguments:
config Configuration file
optional arguments:
-h, --help show this help message and exit
-u URL, --url URL Confluence Url
-a AUTH, --auth AUTH Base64 encoded user:password string
-F, --force Publish not changed page.
-w WATERMARK, --watermark WATERMARK
Overrides the watermarks. Also can be "False" to
remove all watermarks; or "True" to add watermarkswith
default text: "Automatically generated content. Do not
edit directly." on all pages.
-l LINK, --link LINK Overrides page link. If value is "False" then removes
the link.
-ht, --hold-titles Do not change page titles while publishing.
-v, --verbose
```

## Page Maker ##

> conf_page_maker config.yml --auth XXXXXjpwYXNzdXXXXX== --parent-id 52332132

```
usage: conf_page_maker [-h] [-u URL] [-a AUTH] [-pid PARENT_ID] [-v] config
Create Confluence pages and update configuration file with it ids
positional arguments:
config Configuration file
optional arguments:
-h, --help show this help message and exit
-u URL, --url URL Confluence Url
-a AUTH, --auth AUTH Base64 encoded user:password string
-pid PARENT_ID, --parent-id PARENT_ID
Parent page ID in confluence.
-v, --verbose
```
1 change: 1 addition & 0 deletions conf_publisher/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(self):
self.ancestors = list()
self.body = None
self.title = None
self.unused_title = None
super(Page, self).__init__()

def __eq__(self, other):
Expand Down
1 change: 0 additions & 1 deletion conf_publisher/data_providers/sphinx_base_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def __init__(self, root_dir=None, base_dir=None, downloads_dir=None, 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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ def get_source_data(self, filename):
with open(filepath, 'r') as f:
content = json_serializer.load(f)

return content['title'], content['body']
return content.get('title'), content.get('body')
27 changes: 23 additions & 4 deletions conf_publisher/mutators/page_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

class PageMutator(object):

def add(self, page):
def apply_forward(self, page):
raise NotImplemented()

def remove(self, page):
def apply_backward(self, page):
raise NotImplemented()


Expand All @@ -21,10 +21,10 @@ def __init__(self):
def set_param(self, name, value):
self.template_params[name] = value

def add(self, page):
def apply_forward(self, page):
page.body = self.template_prefix + self.template.format(**self.template_params) + self.template_suffix + page.body

def remove(self, page):
def apply_backward(self, page):
page.body = re.sub(re.escape(self.template_prefix) + '.*' + re.escape(self.template_suffix), '', page.body, flags=re.DOTALL)


Expand Down Expand Up @@ -54,3 +54,22 @@ class LinkPageMutator(TemplatePageMutator):
def __init__(self, link):
super(LinkPageMutator, self).__init__()
self.set_param('link', link)


class AnchorPageMutator(PageMutator):
anchor_expression = None
_old_title = u''

def __init__(self, old_title):
if old_title:
self._old_title = ''.join(old_title.split())
self.anchor_expression = re.compile(re.escape(self._old_title))

def apply_forward(self, page):
if not page.title or not page.body or not self.anchor_expression:
return
_title = ''.join(page.title.split())
page.body = self.anchor_expression.sub(_title, page.body, re.I|re.U)

def apply_backward(self, page):
pass
48 changes: 32 additions & 16 deletions conf_publisher/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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
from .mutators.page_mutator import WatermarkPageMutator, LinkPageMutator, AnchorPageMutator


def get_data_provider_class(config):
Expand Down Expand Up @@ -43,11 +43,16 @@ def __init__(self, config, data_provider, page_manager, attachment_manager):

@staticmethod
def _page_title(current_title, new_title, config_title=None, hold_current=False):
result = None
if hold_current:
return current_title
if config_title:
return config_title
return new_title if new_title else current_title
result = current_title
elif config_title:
result = config_title
elif new_title:
result = new_title
else:
result = current_title
return result

def _page(self, current_page, source):
page = copy.copy(current_page)
Expand All @@ -60,33 +65,44 @@ def _page_attachment_file(self, attachment_config):
return self._data_provider.get_attachment(attachment_config.path)

@staticmethod
def _remove_page_mutators(page, page_config):
WatermarkPageMutator(page_config.watermark).remove(page)
LinkPageMutator(page_config.link).remove(page)
def _remove_page_mutators(page, mutators):
for mutator in mutators:
mutator.apply_backward(page)

@staticmethod
def _add_page_mutators(page, page_config):
if page_config.link:
LinkPageMutator(page_config.link).add(page)
def _add_page_mutators(page, mutators):
for mutator in mutators:
mutator.apply_forward(page)

def _init_page_mutators(self, page_config, old_title, hold_titles):
mutators = []

if page_config.link:
mutators.append(LinkPageMutator(page_config.link))
if page_config.watermark:
WatermarkPageMutator(page_config.watermark).add(page)
mutators.append(WatermarkPageMutator(page_config.watermark))
if hold_titles:
mutators.append(AnchorPageMutator(old_title))

return mutators

def _pages_to_update(self, force=False, watermark=False, hold_titles=False):
pages_to_update = []
for page_config in flatten_page_config_list(self._config.pages):
if page_config.id is None:
raise AttributeError('Missed attribute "id"')
current_page = self._page_manager.load(page_config.id)
self._remove_page_mutators(current_page, page_config)

page = self._page(current_page, page_config.source)
page.title = self._page_title(current_page.title, page.title, page_config.title, hold_titles)

mutators = self._init_page_mutators(page_config, page.title, hold_titles)
self._remove_page_mutators(current_page, mutators)

page.title = self._page_title(current_page.title, page.title, page_config.title, hold_titles)
if not force and current_page == page:
continue

self._add_page_mutators(page, page_config)
self._add_page_mutators(page, mutators)

pages_to_update.append(page)
return pages_to_update

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='confluence-publisher',
version='1.1.0',
version='1.2.0',
packages=find_packages(exclude=['tests', 'tests.*']),
include_package_data=True,
license='MIT',
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/titeless_page.fjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"body": "Body",
"alabaster_version": "0.7.6",
"display_toc": true,
"sourcename": "test.rst",
"customsidebar": null,
"current_page_name": "test",
"next": null,
"rellinks": [
[
"genindex",
"General Index",
"I",
"index"
]
],
"meta": {},
"parents": [],
"sidebars": null,
"toc": "",
"prev": null,
"metatags": ""
}
32 changes: 32 additions & 0 deletions tests/mutators/test_anchor_mutator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from unittest import TestCase
from conf_publisher.mutators.page_mutator import AnchorPageMutator
from conf_publisher.confluence import Page


class ConfigLoaderTestCase(TestCase):

# TODO change regular expression to match only anchor
def test_anchor_page_mutator_mutate(self):
original_content = u'''
<p>Default response</p>\n<p>Type: <a class="reference internal" href="#SnowTeq1.0.0-d-c40da07e339eaa512fd6189
f="#SnowTeq1.0.0-d-c40da07e339eaa512fd6189758a42db6"><span>ClubsSerializer</span></a></p>\n<p><strong>Example
<a class="reference internal" href="#SnowTeq1.0.0-d-c40da07e339eaa512fd6189758a42db6"><span>ClubsSerializer</
'''
correct_content = u'''
<p>Default response</p>\n<p>Type: <a class="reference internal" href="#TestPage#2-d-c40da07e339eaa512fd6189
f="#TestPage#2-d-c40da07e339eaa512fd6189758a42db6"><span>ClubsSerializer</span></a></p>\n<p><strong>Example
<a class="reference internal" href="#TestPage#2-d-c40da07e339eaa512fd6189758a42db6"><span>ClubsSerializer</
'''
page_new_title = u'SnowTeq 1.0.0'
page_old_title = u'Test Page #2'

page = Page()
page.body = original_content
# means hold-title = True
page.title = page_old_title

AnchorPageMutator(page_new_title).apply_forward(page)

self.assertEqual(correct_content, page.body)
4 changes: 2 additions & 2 deletions tests/mutators/test_page_mutator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_watermark_page_mutator_add(self):
page = Page()
page.body = content

WatermarkPageMutator(watermark).add(page)
WatermarkPageMutator(watermark).apply_forward(page)

self.assertNotEqual(content, page.body)

Expand All @@ -25,6 +25,6 @@ def test_watermark_page_mutator_remove(self):
WatermarkPageMutator.template.format(watermark=watermark) + \
WatermarkPageMutator.template_suffix + clean_content

WatermarkPageMutator(watermark).remove(page)
WatermarkPageMutator(watermark).apply_backward(page)

self.assertEqual(clean_content, page.body)
Loading

0 comments on commit 249321c

Please sign in to comment.