rawpp.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Purpose: Create a structured HTML view of raw DXF tags - not a CAD drawing!
  2. # Created: 17.01.2018
  3. # Copyright (C) 2018, Manfred Moitzi
  4. # License: MIT License
  5. """
  6. Creates a structured HTML view of raw DXF tags - not a CAD drawing!
  7. """
  8. from typing import Iterable
  9. from ezdxf.tools import escape
  10. from ezdxf.lldxf.tags import group_tags, DXFTag
  11. from ezdxf.lldxf.types import GROUP_MARKERS, BINARY_FLAGS, HEX_HANDLE_CODES
  12. from .dxfpp import tag_type_str, load_resource, with_bitmask, MAX_STR_LEN
  13. TAG_TPL = '<div class="dxf-tag" ><span class="tag-code">{code}</span> <span class="var-type">{type}</span>' \
  14. ' <span class="tag-value">{value}</span></div>'
  15. MARKER_TPL = '<div class="tag-group-marker">{tag}</div>'
  16. CONTROL_TPL = '<div class="tag-control-tag">{tag}</div>'
  17. def rawpp(tagger: Iterable[DXFTag], filename: str) -> str:
  18. def tag2html(tag: DXFTag) -> str:
  19. type_str = tag_type_str(tag.code)
  20. if tag.code in BINARY_FLAGS:
  21. vstr = with_bitmask(tag.value)
  22. else:
  23. vstr = str(tag.value)
  24. if tag.code in HEX_HANDLE_CODES:
  25. vstr = '#' + vstr
  26. if len(vstr) > MAX_STR_LEN:
  27. vstr = vstr[:MAX_STR_LEN - 3] + '...'
  28. return TAG_TPL.format(code=tag.code, value=escape(vstr), type=escape(type_str))
  29. def marker(tag: DXFTag, tag_html: str) -> str:
  30. if tag.code == 0:
  31. return CONTROL_TPL.format(tag=tag_html)
  32. elif tag.code in GROUP_MARKERS:
  33. return MARKER_TPL.format(tag=tag_html)
  34. else:
  35. return tag_html
  36. def tags2html(tags: Iterable[DXFTag]) -> str:
  37. return '\n'.join(marker(tag, tag2html(tag)) for tag in tags)
  38. def groups(tags: Iterable[DXFTag]) -> Iterable[str]:
  39. for group in group_tags(tags, splitcode=0):
  40. yield '<div class="dxf-tag-group">\n{content}\n</div>'.format(content=tags2html(group))
  41. def dxf_control_structures(tags: Iterable[DXFTag]) -> str:
  42. return '\n'.join(groups(tags))
  43. template = load_resource('rawpp.html')
  44. return template.format(
  45. name=filename,
  46. css=load_resource('rawpp.css'),
  47. dxf_file=dxf_control_structures(tagger),
  48. )