Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save NorfeldtKnowit/ddc6942ba0dd9d67d43de1c7c333108d to your computer and use it in GitHub Desktop.

Select an option

Save NorfeldtKnowit/ddc6942ba0dd9d67d43de1c7c333108d to your computer and use it in GitHub Desktop.
Generate table in drawio from table_text.txt
#!/bin/zsh
# Function to escape XML special characters
xml_escape() {
local string="${1}"
string="${string//&/&}"
string="${string//</&lt;}"
string="${string//>/&gt;}"
string="${string//\"/&quot;}"
string="${string//\'/&apos;}"
echo "$string"
}
# Function to generate draw.io XML for each table item
generate_item() {
local title=$(xml_escape "$1")
local description=$(xml_escape "$2")
local y_position="$3"
local has_title="$4"
if [[ "$has_title" == "true" ]]; then
echo "<mxCell id='item_$RANDOM' value='&lt;b&gt;$title&lt;/b&gt;: $description' style='text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;' vertex='1' parent='2'>"
else
echo "<mxCell id='item_$RANDOM' value='$title' style='text;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;rotatable=0;whiteSpace=wrap;html=1;' vertex='1' parent='2'>"
fi
echo "<mxGeometry y='$y_position' height='30' as='geometry'/>" # Removed width and x attributes
echo "</mxCell>"
}
# Main function to parse input text and generate draw.io table XML
generate_drawio_table() {
local file_path="$1"
local y_position=0 # Start at 0 and increment
{
read -r table_title
table_title=$(xml_escape "$table_title")
echo "<mxGraphModel><root>"
echo "<mxCell id='0'/>"
echo "<mxCell id='1' parent='0'/>"
echo "<mxCell id='2' value='&lt;b&gt;$table_title&lt;/b&gt;' style='swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=30;horizontalStack=0;resizeParent=1;resizeParentMax=0;resizeLast=0;collapsible=1;marginBottom=0;whiteSpace=wrap;html=1;' vertex='1' parent='1'>"
echo "<mxGeometry x='-510' y='0' width='500' height='90' as='geometry'/>"
echo "</mxCell>"
# Process each line
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == *:* ]]; then
IFS=: read -r item_title item_description <<< "$line"
generate_item "$item_title" "$item_description" $y_position "true"
else
generate_item "$line" "" $y_position "false"
fi
((y_position+=30))
done
} < "$file_path"
echo "</root></mxGraphModel>"
}
# File path for table entries
file_path="table_text.txt"
# Generate the drawio XML and copy it to the clipboard
generate_drawio_table "$file_path" | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment