Skip to content

Instantly share code, notes, and snippets.

@salticus
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save salticus/9519167 to your computer and use it in GitHub Desktop.

Select an option

Save salticus/9519167 to your computer and use it in GitHub Desktop.
#!/bin/bash
# https://gist.github.com/salticus/9519167
# License GPLv3
# Create a svg representation of a django template inheritance tree
# use web browser, eog (if you're on ubuntu) or whatever to view
TEMPLATE_DIR=templates
DOT_DIR="$( pwd )"
makedot(){
local _dot_file="$1"
local _svg_file="${1%.dot}.svg"
local _contents="$2"
echo "DOT_FILE: $_dot_file"
echo "SVG_FILE: $_svg_file"
echo "Make dot"
echo "digraph {" > "$_dot_file"
echo "rankdir=LR" >> "$_dot_file"
cat "$_contents" >> "$_dot_file"
echo "}" >> "$_dot_file"
dot -Tsvg "$_dot_file" -o"$_svg_file"
}
pushd "$TEMPLATE_DIR"
# create A.html -> B.html
__extends="$( mktemp )"
__includes="$( mktemp )"
__everything="$( mktemp )"
rgrep extends * | perl -pe "s/(.*):.*extends\s+[\"'](.*)[\"'].*/\"\1\" -> \"\2\"/" >> "$__extends"
# create C_include.html [style=filled... etc
rgrep '% include\>' * | perl -pe "s/(.*):.*include\s+[\"'](.*?)[\"'].*/\"\2\" \[style=filled color=yellow shape=diamond\]/" >> "$__includes"
# create A.html -> C_include.html [style=dotted]
rgrep '% include\>' * | perl -pe "s/(.*):.*include\s+[\"'](.*?)[\"'].*/\"\1\" -> \"\2\" \[style=dotted\]/" >> "$__includes"
# create just template inheritance, just includes, and everything
makedot "${DOT_DIR}/template-inheritance.dot" "$__extends"
makedot "${DOT_DIR}/template-includes.dot" "$__includes"
cat "$__extends" "$__includes" > "$__everything"
makedot "${DOT_DIR}/template-everything.dot" "$__everything"
#!/bin/bash
# https://gist.github.com/salticus/9519167
# License GPLv3
# Create a svg representation of a django template inheritance tree
# use web browser, eog (if you're on ubuntu) or whatever to view
TEMPLATE_DIR=templates
DOT_DIR="$( pwd )"
makedot(){
local _dot_file="$1"
local _svg_file="${1%.dot}.svg"
local _contents="$2"
echo "DOT_FILE: $_dot_file"
echo "SVG_FILE: $_svg_file"
echo "Make dot"
echo "digraph {" > "$_dot_file"
echo "rankdir=LR" >> "$_dot_file"
cat "$_contents" >> "$_dot_file"
echo "}" >> "$_dot_file"
dot -Tsvg "$_dot_file" -o"$_svg_file"
}
pushd "$TEMPLATE_DIR"
# create A.html -> B.html
__extends="$( mktemp )"
__includes="$( mktemp )"
__everything="$( mktemp )"
rgrep extends * | perl -pe "s/(.*):.*extends\s+[\"'](.*)[\"'].*/\"\1\" -> \"\2\"/" >> "$__extends"
# create C_include.html [style=filled... etc
rgrep '% include\>' * | perl -pe "s/(.*):.*include\s+[\"'](.*?)[\"'].*/\"\2\" \[style=filled color=yellow shape=diamond\]/" >> "$__includes"
# create A.html -> C_include.html [style=dotted]
rgrep '% include\>' * | perl -pe "s/(.*):.*include\s+[\"'](.*?)[\"'].*/\"\1\" -> \"\2\" \[style=dotted\]/" >> "$__includes"
# create just template inheritance, just includes, and everything
makedot "${DOT_DIR}/template-inheritance.dot" "$__extends"
makedot "${DOT_DIR}/template-includes.dot" "$__includes"
cat "$__extends" "$__includes" > "$__everything"
makedot "${DOT_DIR}/template-everything.dot" "$__everything"
rm "$__extends" "$__includes" "$__everything"
dot -Tsvg "$DOT_FILE" -o"$SVG_FILE"
rm "$__extends" "$__includes" "$__everything"
#!/bin/bash
# https://gist.github.com/salticus/9519167
# License GPLv3
# Create a svg representation of a django template inheritance tree
# use web browser, eog (if you're on ubuntu) or whatever to view
TEMPLATE_DIR=templates
DOT_DIR="$( pwd )"
makedot(){
local _dot_file="$1"
local _svg_file="${1%.dot}.svg"
local _contents="$2"
echo "DOT_FILE: $_dot_file"
echo "SVG_FILE: $_svg_file"
echo "Make dot"
echo "digraph {" > "$_dot_file"
echo "rankdir=LR" >> "$_dot_file"
cat "$_contents" >> "$_dot_file"
echo "}" >> "$_dot_file"
dot -Tsvg "$_dot_file" -o"$_svg_file"
}
pushd "$TEMPLATE_DIR"
# create A.html -> B.html
__extends="$( mktemp )"
__includes="$( mktemp )"
__everything="$( mktemp )"
rgrep extends * | perl -pe "s/(.*):.*extends\s+[\"'](.*)[\"'].*/\"\1\" -> \"\2\"/" >> "$__extends"
# create C_include.html [style=filled... etc
rgrep '% include\>' * | perl -pe "s/(.*):.*include\s+[\"'](.*?)[\"'].*/\"\2\" \[style=filled color=yellow shape=diamond\]/" >> "$__includes"
# create A.html -> C_include.html [style=dotted]
rgrep '% include\>' * | perl -pe "s/(.*):.*include\s+[\"'](.*?)[\"'].*/\"\1\" -> \"\2\" \[style=dotted\]/" >> "$__includes"
# create just template inheritance, just includes, and everything
makedot "${DOT_DIR}/template-inheritance.dot" "$__extends"
makedot "${DOT_DIR}/template-includes.dot" "$__includes"
cat "$__extends" "$__includes" > "$__everything"
makedot "${DOT_DIR}/template-everything.dot" "$__everything"
rm "$__extends" "$__includes" "$__everything"
popd
@cellofellow
Copy link

This is really useful. I'd like to see it also show included templates, perhaps with different colored edges or something.

@salticus
Copy link
Author

What I really need is a searchable file that shows the inheritance path for each template. Something like

ferrari.html > car.html > vehicle.html
buick.html > car.html > vehicle.html
scooter.html > vehicle.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment