public Map<String, Resource> indexResources(String rootURI) { String entity = get(uri(rootURI)); Map<String, Resource> index = createIndexFromEntity(rootURI, entity); addBreadcrumbs(index); return index; }
public Map<String, Resource> indexResources(String rootURI, String rootPath, String syntax, String includes) { Map<String, Resource> index = createIndexFromPaths(rootURI, rootPath, syntax, includes); addBreadcrumbs(index); return index; }
private void addPath(String rootURI, String rootPath, String path, String syntax, Map<String, Resource> index) { File file = new File(path); String name = resolveName(fileNameWithoutExt(file)); String parentName = parentName(file, rootPath); String uri = fullPath(rootURI, name); Resource resource = new Resource(uri, name, parentName); resource.setContent(contentOf(file)); if ( isNotBlank(syntax) ){ resource.setSyntax(syntax); } index.put(name, resource); if (parentName != null) { addPath(rootURI, rootPath, file.getParent(), syntax, index); } }
protected Map<String, Resource> createIndexFromPaths(String rootURI, String rootPath, String syntax, String includes) { Map<String, Resource> index = new HashMap<>(); List<String> paths = new StoryFinder().findPaths( codeLocationFromPath(rootPath), includes, EMPTY); for (String path : paths) { addPath(rootURI, rootPath, fullPath(rootPath, path), syntax, index); } return index; }
protected void addBreadcrumbs(Map<String, Resource> index) { for (Resource resource : index.values()) { List<String> breadcrumbs = new ArrayList<>(); collectBreadcrumbs(breadcrumbs, resource, index); resource.setBreadcrumbs(breadcrumbs); } }
private String parentName(File file, String rootPath) { File parent = file.getParentFile(); if (parent != null && !normalisedPathOf(parent).equals(rootPath)) { return resolveName(parent.getName()); } return null; }
private void collectBreadcrumbs(List<String> breadcrumbs, Resource resource, Map<String, Resource> index) { if (resource.hasParent()) { String parentName = resource.getParentName(); breadcrumbs.add(0, parentName); Resource parent = index.get(parentName); if (parent != null) { collectBreadcrumbs(breadcrumbs, parent, index); } } }