cds2md 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cds2md
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Confluence Data Center to Markdown Utility
5
5
  Author: James Knight
6
6
  Author-email: git@jdknight.me
@@ -22,7 +22,7 @@ License-File: LICENSE
22
22
  Requires-Dist: beautifulsoup4
23
23
  Requires-Dist: httpx
24
24
  Requires-Dist: python-slugify
25
- Project-URL: Download, https://pypi.python.org/pypi/cds2md
25
+ Project-URL: Download, https://pypi.python.org/project/cds2md
26
26
  Project-URL: Homepage, https://github.com/jdknight/cds2md
27
27
  Project-URL: Source, https://github.com/jdknight/cds2md
28
28
 
@@ -123,6 +123,12 @@ options:
123
123
  anchor formatting to use (default: default)
124
124
  --tweak-html-images force all images to be generated as html-based images
125
125
  --tweak-html-tables force all tables to be generated as html-based tables
126
+ --tweak-image-ignore-size
127
+ ignore image sizes for consideration to change markdown
128
+ --tweak-reference-extensionless
129
+ avoid adding the .md extension in references
130
+ --tweak-reference-relative-hint
131
+ explicitly add a ./ prefix for relative references
126
132
  --version show program's version number and exit
127
133
  --wrap WRAP hint to the number of characters to wrap to
128
134
  ```
@@ -95,6 +95,12 @@ options:
95
95
  anchor formatting to use (default: default)
96
96
  --tweak-html-images force all images to be generated as html-based images
97
97
  --tweak-html-tables force all tables to be generated as html-based tables
98
+ --tweak-image-ignore-size
99
+ ignore image sizes for consideration to change markdown
100
+ --tweak-reference-extensionless
101
+ avoid adding the .md extension in references
102
+ --tweak-reference-relative-hint
103
+ explicitly add a ./ prefix for relative references
98
104
  --version show program's version number and exit
99
105
  --wrap WRAP hint to the number of characters to wrap to
100
106
  ```
@@ -1,4 +1,4 @@
1
1
  # SPDX-License-Identifier: BSD-2-Clause
2
2
  # Copyright jdknight
3
3
 
4
- __version__ = '0.2.0'
4
+ __version__ = '0.3.0'
@@ -128,6 +128,14 @@ def convert(env: Environment, opts: Namespace) -> bool:
128
128
 
129
129
  root_path = page_path.relative_to(opts.output)
130
130
  page_full_slug = root_path.as_posix()
131
+
132
+ # If directly placing wiki content on a site and not using a
133
+ # documentation generator, select wiki engines (e.g GitLab Wiki) may
134
+ # treat .md suffixed entries are raw links to markdown pages over a
135
+ # link to the page. Provide a hint to allow stripping the extension.
136
+ if opts.tweak_reference_extensionless:
137
+ page_full_slug = page_full_slug.removesuffix('.md')
138
+
131
139
  title2target[page_cache.title] = page_full_slug
132
140
 
133
141
  for attachment_cache in page_cache.attachments:
@@ -214,6 +214,21 @@ cds2md
214
214
  action='store_true',
215
215
  help='force all tables to be generated as html-based tables',
216
216
  )
217
+ parser.add_argument(
218
+ '--tweak-image-ignore-size',
219
+ action='store_true',
220
+ help='ignore image sizes for consideration to change markdown',
221
+ )
222
+ parser.add_argument(
223
+ '--tweak-reference-extensionless',
224
+ action='store_true',
225
+ help='avoid adding the .md extension in references',
226
+ )
227
+ parser.add_argument(
228
+ '--tweak-reference-relative-hint',
229
+ action='store_true',
230
+ help='explicitly add a ./ prefix for relative references',
231
+ )
217
232
  parser.add_argument(
218
233
  '--version',
219
234
  action='version',
@@ -51,6 +51,7 @@ class Translator:
51
51
  self.title = title
52
52
  self.title2target = ctx.title2target
53
53
  self.userid2name = ctx.userid2name
54
+ self.wrap_context = [True]
54
55
 
55
56
  def walk(self, node: PageElement) -> None:
56
57
  if isinstance(node, Tag):
@@ -96,6 +97,10 @@ class Translator:
96
97
  # reduce the wrap if we are indenting any text contents
97
98
  wrap = max(self.opts.wrap - self.indent_offset[-1], 0)
98
99
 
100
+ # always disable wrap if context does not support it
101
+ if not self.wrap_context[-1]:
102
+ wrap = 0
103
+
99
104
  # check if we are configured to wrap content
100
105
  if wrap > 0:
101
106
  # track any leading/trailing whitespace content, which we want to
@@ -149,11 +154,22 @@ class Translator:
149
154
  self.append(f']({href})')
150
155
 
151
156
  def visit_ac_image(self, node: Tag) -> None:
157
+ attribs = {
158
+ 'alt': cast('str', node.get('ac:alt', '')),
159
+ }
160
+
161
+ height = node.get('ac:height')
162
+ if height:
163
+ attribs['height'] = cast('str', height)
164
+
165
+ width = node.get('ac:width')
166
+ if width:
167
+ attribs['width'] = cast('str', width)
168
+
152
169
  attachment_url = node.find('ri:url', recursive=False)
153
170
  if attachment_url:
154
- alt = cast('str', node.get('ac:alt', ''))
155
- href = cast('str', attachment_url.get('ri:value', '#'))
156
- self._build_image(href, alt)
171
+ attribs['src'] = cast('str', attachment_url.get('ri:value', '#'))
172
+ self._build_image(attribs)
157
173
  raise SkipNode
158
174
 
159
175
  attachment_node = node.find('ri:attachment', recursive=False)
@@ -184,16 +200,22 @@ class Translator:
184
200
  logger.warning(f'unexpected image with no mapped attachment: {key}')
185
201
  raise SkipNode
186
202
 
187
- alt = cast('str', node.get('alt', ''))
188
- resolved_ref = self._resolve_ref(attachment_ref)
189
- self._build_image(resolved_ref, alt)
203
+ attribs['src'] = self._resolve_ref(attachment_ref)
204
+ self._build_image(attribs)
190
205
  raise SkipNode
191
206
 
192
- def _build_image(self, href: str, alt: str) -> None:
193
- if self.is_html_mode() or self.opts.tweak_html_images:
194
- self.append(f'<img href="{href}" alt="{alt}">')
207
+ def _build_image(self, attribs: dict[str, str]) -> None:
208
+ has_size = not self.opts.tweak_image_ignore_size and \
209
+ ('height' in attribs or 'width' in attribs)
210
+
211
+ if self.is_html_mode() or self.opts.tweak_html_images or has_size:
212
+ if not attribs['alt']:
213
+ del attribs['alt']
214
+ tag_attribs = ' '.join(f'{k}="{v}"'
215
+ for k, v in sorted(attribs.items()))
216
+ self.append(f'<img {tag_attribs}>')
195
217
  else:
196
- self.append(f'![{alt}]({href})')
218
+ self.append(f'![{attribs["alt"]}]({attribs["src"]})')
197
219
 
198
220
  def visit_ac_inline_comment_marker(self, node: Tag) -> None:
199
221
  pass
@@ -388,7 +410,7 @@ class Translator:
388
410
  case AdmonitionType.GITHUB | AdmonitionType.GITLAB:
389
411
  self.append('> [!NOTE]')
390
412
  case AdmonitionType.MYST:
391
- self.append(':::{note}')
413
+ self.append('```{note}')
392
414
  case 'note':
393
415
  match self.admonition_type:
394
416
  case AdmonitionType.DOCUSAURUS:
@@ -396,7 +418,7 @@ class Translator:
396
418
  case AdmonitionType.GITHUB | AdmonitionType.GITLAB:
397
419
  self.append('> [!WARNING]')
398
420
  case AdmonitionType.MYST:
399
- self.append(':::{warning}')
421
+ self.append('```{warning}')
400
422
  case 'tip':
401
423
  match self.admonition_type:
402
424
  case AdmonitionType.DOCUSAURUS:
@@ -404,7 +426,7 @@ class Translator:
404
426
  case AdmonitionType.GITHUB | AdmonitionType.GITLAB:
405
427
  self.append('> [!TIP]')
406
428
  case AdmonitionType.MYST:
407
- self.append(':::{tip}')
429
+ self.append('```{tip}')
408
430
  case 'warning':
409
431
  match self.admonition_type:
410
432
  case AdmonitionType.DOCUSAURUS:
@@ -412,7 +434,7 @@ class Translator:
412
434
  case AdmonitionType.GITHUB | AdmonitionType.GITLAB:
413
435
  self.append('> [!CAUTION]')
414
436
  case AdmonitionType.MYST:
415
- self.append(':::{caution}')
437
+ self.append('```{caution}')
416
438
 
417
439
  # track if this type of admonition will be prefixed with ">"
418
440
  is_admonition_prefixed = False
@@ -618,10 +640,14 @@ class Translator:
618
640
  self.output[-1] = self.output[-1].rstrip('\n')
619
641
 
620
642
  match self.admonition_type:
621
- case AdmonitionType.DOCUSAURUS | AdmonitionType.MYST:
643
+ case AdmonitionType.DOCUSAURUS:
622
644
  self.append(':::')
623
645
  self.newline(count=2)
624
646
 
647
+ case AdmonitionType.MYST:
648
+ self.append('```')
649
+ self.newline(count=2)
650
+
625
651
  was_admonition_prefixed = self.admonition_prefix_context.pop()
626
652
  if was_admonition_prefixed and self.admonition_prefix_pending:
627
653
  self.output.pop()
@@ -708,7 +734,8 @@ class Translator:
708
734
  if self.is_html_mode():
709
735
  self.append('<code>')
710
736
  else:
711
- self.append(f'`{node.text}`')
737
+ if node.text:
738
+ self.append(f'`{node.text}`')
712
739
  raise SkipNode
713
740
 
714
741
  def depart_code(self, node: Tag) -> None:
@@ -751,14 +778,15 @@ class Translator:
751
778
  def visit_dt(self, node: Tag) -> None:
752
779
  if self.is_html_mode():
753
780
  self.append('<dt>')
754
- else:
781
+ elif not node.find('strong'):
755
782
  self.append('**')
756
783
 
757
784
  def depart_dt(self, node: Tag) -> None:
758
785
  if self.is_html_mode():
759
786
  self.append('</dt>')
760
787
  else:
761
- self.append('**')
788
+ if not node.find('strong'):
789
+ self.append('**')
762
790
  self.newline(count=2)
763
791
 
764
792
  def visit_li(self, node: Tag) -> None:
@@ -785,17 +813,17 @@ class Translator:
785
813
  def visit_em(self, node: Tag) -> None:
786
814
  if self.is_html_mode():
787
815
  self.append('<em>')
788
- else:
816
+ elif node.text.strip():
789
817
  self.append('*')
790
818
 
791
819
  def depart_em(self, node: Tag) -> None:
792
820
  if self.is_html_mode():
793
821
  self.append('</em>')
794
- else:
822
+ elif node.text.strip():
795
823
  self.append('*')
796
824
 
797
825
  def visit_h1(self, node: Tag) -> None:
798
- self._process_header_anchors(node)
826
+ self._visit_header(node)
799
827
 
800
828
  if self.is_html_mode():
801
829
  self.append('<h1>')
@@ -808,8 +836,10 @@ class Translator:
808
836
  else:
809
837
  self.newline(count=2)
810
838
 
839
+ self._depart_header(node)
840
+
811
841
  def visit_h2(self, node: Tag) -> None:
812
- self._process_header_anchors(node)
842
+ self._visit_header(node)
813
843
 
814
844
  if self.is_html_mode():
815
845
  self.append('<h2>')
@@ -822,8 +852,10 @@ class Translator:
822
852
  else:
823
853
  self.newline(count=2)
824
854
 
855
+ self._depart_header(node)
856
+
825
857
  def visit_h3(self, node: Tag) -> None:
826
- self._process_header_anchors(node)
858
+ self._visit_header(node)
827
859
 
828
860
  if self.is_html_mode():
829
861
  self.append('<h3>')
@@ -836,8 +868,10 @@ class Translator:
836
868
  else:
837
869
  self.newline(count=2)
838
870
 
871
+ self._depart_header(node)
872
+
839
873
  def visit_h4(self, node: Tag) -> None:
840
- self._process_header_anchors(node)
874
+ self._visit_header(node)
841
875
 
842
876
  if self.is_html_mode():
843
877
  self.append('<h4>')
@@ -850,8 +884,10 @@ class Translator:
850
884
  else:
851
885
  self.newline(count=2)
852
886
 
887
+ self._depart_header(node)
888
+
853
889
  def visit_h5(self, node: Tag) -> None:
854
- self._process_header_anchors(node)
890
+ self._visit_header(node)
855
891
 
856
892
  if self.is_html_mode():
857
893
  self.append('<h5>')
@@ -864,8 +900,10 @@ class Translator:
864
900
  else:
865
901
  self.newline(count=2)
866
902
 
903
+ self._depart_header(node)
904
+
867
905
  def visit_h6(self, node: Tag) -> None:
868
- self._process_header_anchors(node)
906
+ self._visit_header(node)
869
907
 
870
908
  if self.is_html_mode():
871
909
  self.append('<h6>')
@@ -878,7 +916,9 @@ class Translator:
878
916
  else:
879
917
  self.newline(count=2)
880
918
 
881
- def _process_header_anchors(self, node: Tag) -> None:
919
+ self._depart_header(node)
920
+
921
+ def _visit_header(self, node: Tag) -> None:
882
922
  # find all anchors residing in the headers
883
923
  for o in node.find_all('ac:structured-macro', {'ac:name': 'anchor'}):
884
924
  self._process_anchor(o)
@@ -887,6 +927,11 @@ class Translator:
887
927
  if not self.is_html_mode() and not self.last_ends_with_nl():
888
928
  self.newline()
889
929
 
930
+ self.wrap_context.append(False)
931
+
932
+ def _depart_header(self, node: Tag) -> None:
933
+ self.wrap_context.pop()
934
+
890
935
  def visit_hr(self, node: Tag) -> None:
891
936
  if self.is_html_mode():
892
937
  self.append('<hr>')
@@ -900,7 +945,18 @@ class Translator:
900
945
  raise SkipNode
901
946
 
902
947
  def visit_img(self, node: Tag) -> None:
903
- if self.is_html_mode() or self.opts.tweak_html_images:
948
+ has_size = False
949
+
950
+ if not self.opts.tweak_image_ignore_size:
951
+ height = node.get('height')
952
+ if height:
953
+ has_size = True
954
+
955
+ width = node.get('width')
956
+ if width:
957
+ has_size = True
958
+
959
+ if self.is_html_mode() or self.opts.tweak_html_images or has_size:
904
960
  self.append(f'{node}')
905
961
  else:
906
962
  src = node.get('src', '')
@@ -985,13 +1041,13 @@ class Translator:
985
1041
  def visit_s(self, node: Tag) -> None:
986
1042
  if self.is_html_mode():
987
1043
  self.append('<s>')
988
- else:
1044
+ elif node.text.strip():
989
1045
  self.append('~~')
990
1046
 
991
1047
  def depart_s(self, node: Tag) -> None:
992
1048
  if self.is_html_mode():
993
1049
  self.append('</s>')
994
- else:
1050
+ elif node.text.strip():
995
1051
  self.append('~~')
996
1052
 
997
1053
  def visit_span(self, node: Tag) -> None:
@@ -1007,13 +1063,13 @@ class Translator:
1007
1063
  def visit_strong(self, node: Tag) -> None:
1008
1064
  if self.is_html_mode():
1009
1065
  self.append('<strong>')
1010
- else:
1066
+ elif node.text.strip():
1011
1067
  self.append('**')
1012
1068
 
1013
1069
  def depart_strong(self, node: Tag) -> None:
1014
1070
  if self.is_html_mode():
1015
1071
  self.append('</strong>')
1016
- else:
1072
+ elif node.text.strip():
1017
1073
  self.append('**')
1018
1074
 
1019
1075
  visit_b = visit_strong
@@ -1032,6 +1088,20 @@ class Translator:
1032
1088
  self.append('</sup>')
1033
1089
 
1034
1090
  def visit_table(self, node: Tag) -> None:
1091
+ # special handling -- single task list
1092
+ #
1093
+ # Confluence pages may wrap a task list with table. If a table is
1094
+ # only containing a task list, we do not need to wrap the list in
1095
+ # such a table. Instead, we will jump walking to the task list to
1096
+ # process that node set, and skip process any table-related tags.
1097
+ if len(node.find_all('tr')) == 1 or len(node.find_all('td')) == 1:
1098
+ tr_node = node.find('td')
1099
+ assert tr_node
1100
+ task_list_node = tr_node.find('ac:task-list')
1101
+ if task_list_node:
1102
+ self.walk(task_list_node)
1103
+ raise SkipNode
1104
+
1035
1105
  html_mode = self.opts.tweak_html_tables
1036
1106
 
1037
1107
  # if we are already in an html table, continue this table as an html
@@ -1057,6 +1127,7 @@ class Translator:
1057
1127
  # force an html format if:
1058
1128
  if node.find([
1059
1129
  # we have any lists
1130
+ 'ac:task-list',
1060
1131
  'li',
1061
1132
  # we have a nested table
1062
1133
  'table',
@@ -1290,7 +1361,15 @@ class Translator:
1290
1361
  logger.debug(f'(ref) {page_slug.as_posix()} -> '
1291
1362
  f'{target_path.as_posix()} : {ref.as_posix()}')
1292
1363
 
1293
- return ref.as_posix()
1364
+ final_ref = ref.as_posix()
1365
+
1366
+ # select wiki engines may require a "./" hint for relative references
1367
+ # (e.g. GitLab Wiki)
1368
+ if self.opts.tweak_reference_relative_hint:
1369
+ final_ref = f'./{final_ref}'
1370
+
1371
+ return final_ref
1372
+
1294
1373
 
1295
1374
  def _process_anchor(self, node: Tag) -> None:
1296
1375
  anchor_id = None
@@ -43,5 +43,5 @@ cds2md = 'cds2md.__main__:main'
43
43
 
44
44
  [project.urls]
45
45
  Homepage = 'https://github.com/jdknight/cds2md'
46
- Download = 'https://pypi.python.org/pypi/cds2md'
46
+ Download = 'https://pypi.python.org/project/cds2md'
47
47
  Source = 'https://github.com/jdknight/cds2md'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes