Skip to content

Commit

Permalink
Fix auto_css (when toc, stylesheets were lost)
Browse files Browse the repository at this point in the history
  • Loading branch information
openandclose committed Nov 12, 2019
1 parent 21822ba commit 12e73fc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Changelog
Important changes (that users especially need to know) are marked by '**[!]**.'


**Unreleased:**
---------------

**Fix:**

Fix auto_css (when toc, stylesheets were lost)


v0.2.0 (2019-11-10)
-------------------

Expand Down
4 changes: 4 additions & 0 deletions docs/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ Config Files

<link href="[...]/css/auto/developer.mozilla.org.css" rel="stylesheet">

Note when running ``toc``, a parent html collects
all children htmls' auto_css stylesheets in order.
So when creating a mixed-sites-pdf, you should control the effects carefully.

.. dword:: process directory

``userdir`` can also have 'process' sub directory. For example ::
Expand Down
16 changes: 9 additions & 7 deletions tosixinch/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
DEFAULT_DOCTYPE = '<!DOCTYPE html>'
DEFAULT_TITLE = 'notitle'

EXTERNAL_CSS = '<link href="%s" rel="stylesheet">'
EXTERNAL_CSS = '<link class="tsi-auto-css" href="%s" rel="stylesheet">'


def is_html(fname, text, min_chars=4096):
Expand Down Expand Up @@ -181,15 +181,17 @@ def _append_bodies(root, rootname, fnames, codings, errors):

def _relink_component(doc, rootname, fname):
for el, url in iter_component(doc):
# print(url)
url = posixpath.join(posixpath.dirname(fname), url)
# print(url, 'joined')
url = posixpath.relpath(url, start=posixpath.dirname(rootname))
url = posixpath.normpath(url)
# print(url, 'relpathed')
url = _relink(url, fname, rootname)
el.attrib['src'] = url


def _relink(url, prev_base, new_base):
url = posixpath.join(posixpath.dirname(prev_base), url)
url = posixpath.relpath(url, start=posixpath.dirname(new_base))
url = posixpath.normpath(url)
return url


class Content(object):
"""Represent general content."""

Expand Down
18 changes: 17 additions & 1 deletion tosixinch/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"""

import logging
import os
import re
import sys

from tosixinch import location
from tosixinch.process import gen
from tosixinch import system
from tosixinch.content import build_new_html, slugify, _relink_component
from tosixinch.content import (
build_new_html, slugify, _relink_component, _relink)

logger = logging.getLogger(__name__)

Expand All @@ -30,6 +32,7 @@ def __init__(self, level, url, title, root=None, platform=sys.platform):
self.root = root or self
self.last = False
self._doc = None
self.sitecss = []

def _is_local(self, url):
if url.strip().startswith(DIRECTIVE_PREFIX):
Expand All @@ -52,6 +55,18 @@ def _create_doc(self):
else:
self._doc = system.HtmlReader(self.fnew).read()

def _append_auto_css(self):
for el in self.doc.xpath('//head/link[@class="tsi-auto-css"]'):
href = el.get('href') or ''
if href:
site = os.path.basename(href)[:-4]
if site and site in self.root.sitecss:
continue
href = _relink(href, self.slash_fnew, self.root.slash_fnew)
el.set('href', href)
self.root.doc.head.append(el)
self.root.sitecss.append(site)

# TODO: consider using content.merge_htmls().
def _append_body(self):
for t in self.doc.xpath('//body'):
Expand All @@ -63,6 +78,7 @@ def _append_body(self):

def write(self):
if self.root is not self:
self._append_auto_css()
self._append_body()

if self.last:
Expand Down

0 comments on commit 12e73fc

Please sign in to comment.