Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Merge pathname breadcrumbs. Fix #130. Fixes 758628.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrose committed Jun 13, 2013
2 parents 91ae737 + e55e6e1 commit 5045753
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 22 deletions.
72 changes: 52 additions & 20 deletions dxr/build.py
Expand Up @@ -21,6 +21,33 @@
from dxr.utils import load_template_env, connect_database, open_log


def linked_pathname(path, tree_name):
"""Return a list of (server-relative URL, subtree name) tuples that can be
used to display linked path components in the headers of file or folder
pages.
:arg path: The path that will be split
"""
# Hold the root of the tree:
components = [('/%s/source' % tree_name, tree_name)]

# Populate each subtree:
dirs = path.split(os.sep) # TODO: Trips on \/ in path.

# A special case when we're dealing with the root tree. Without
# this, it repeats:
if not path:
return components

for idx in range(1, len(dirs)+1):
subtree_path = os.path.join('/', tree_name, 'source', *dirs[:idx])
subtree_name = os.path.split(subtree_path)[1] or tree_name
components.append((subtree_path, subtree_name))

return components


def build_instance(config_path, nb_jobs=None, tree=None):
"""Build a DXR instance.
Expand Down Expand Up @@ -274,22 +301,24 @@ def build_folder(tree, conn, folder, indexed_files, indexed_folders):
dst_path = os.path.join(tree.target_folder,
folder,
tree.config.directory_index)

_fill_and_write_template(
jinja_env,
'folder.html',
dst_path,
{# Common template variables:
'wwwroot': tree.config.wwwroot,
'tree': tree.name,
'trees': [t.name for t in tree.config.trees],
'config': tree.config.template_parameters,
'generated_date': tree.config.generated_date,
'wwwroot': tree.config.wwwroot,
'tree': tree.name,
'trees': [t.name for t in tree.config.trees],
'config': tree.config.template_parameters,
'generated_date': tree.config.generated_date,
'paths_and_names': linked_pathname(folder, tree.name),

# Folder template variables:
'name': name,
'path': folder,
'folders': folders,
'files': files})
# Folder template variables:
'name': name,
'path': folder,
'folders': folders,
'files': files})


def _join_url(*args):
Expand Down Expand Up @@ -462,19 +491,22 @@ def htmlify(tree, conn, icon, path, text, dst_path, plugins):
env = load_template_env(tree.config.temp_folder,
tree.config.template_folder)
tmpl = env.get_template('file.html')

arguments = {
# Set common template variables
'wwwroot': tree.config.wwwroot,
'tree': tree.name,
'trees': [t.name for t in tree.config.trees],
'config': tree.config.template_parameters,
'wwwroot': tree.config.wwwroot,
'tree': tree.name,
'trees': [t.name for t in tree.config.trees],
'config': tree.config.template_parameters,
'generated_date': tree.config.generated_date,
# Set file template variables
'icon': icon,
'path': path,
'name': os.path.basename(path),
'lines': build_lines(tree, conn, path, text, htmlifiers),
'sections': build_sections(tree, conn, path, text, htmlifiers)

# Set file template variables
'paths_and_names': linked_pathname(path, tree.name),
'icon': icon,
'path': path,
'name': os.path.basename(path),
'lines': build_lines(tree, conn, path, text, htmlifiers),
'sections': build_sections(tree, conn, path, text, htmlifiers)
}
# Fill-in variables and dump to file with utf-8 encoding
tmpl.stream(**arguments).dump(dst_path, encoding='utf-8')
Expand Down
16 changes: 16 additions & 0 deletions dxr/static/layout.css
Expand Up @@ -340,3 +340,19 @@ table.folder-content {
width: 100%;
text-align: center;
}

div.file-name {
padding: 0 1em;
font-weight: bold;
}
div.file-name a {
text-decoration: none;
}
div.file-name span.slash {
padding: 0 0.5em;
}

div.file-name a:hover {
text-decoration: underline;
background-color: #eee;
}
8 changes: 6 additions & 2 deletions dxr/templates/layout.html
Expand Up @@ -60,11 +60,15 @@
</div>
<div class="shown-with-results">
<div id="tree-tip">
Browsing {% if not showing_tree_menu %}<b>{{ tree }}</b>{% endif %}
<div class="file-name">
{%- for path, name in paths_and_names -%}
<span class="slash">/</span><a href="{{ path }}">{{ name }}</a>
{%- endfor -%}
</div>
</div>
<div id="tip">
{%- block tip -%}
Enter search query here
<label for="query">Enter search query here</label>
{%- endblock -%}
</div>
</div>
Expand Down
20 changes: 20 additions & 0 deletions tests/test_units.py
@@ -0,0 +1,20 @@
"""Unit tests that don't fit anywhere else"""

from unittest import TestCase

from nose.tools import eq_

from dxr.build import linked_pathname


class LinkedPathnameTests(TestCase):
def test_deep_path(self):
"""Make sure paths more than one level deep are linked correctly."""
eq_(linked_pathname('hey/thankyou', 'code'),
[('/code/source', 'code'),
('/code/source/hey', 'hey'),
('/code/source/hey/thankyou', 'thankyou')])

def test_root_folder(self):
"""Make sure the root folder is treated correctly."""
eq_(linked_pathname('', 'stuff'), [('/stuff/source', 'stuff')])

0 comments on commit 5045753

Please sign in to comment.