254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382 | def main():
"""
Main program
"""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - \
%(levelname)s - %(funcName)s [%(lineno)d] - \
\t%(message)s",
)
LOGGER = logging.getLogger(__name__)
# ArgumentParser to parse arguments and options
PARSER = get_parser()
ARGS = PARSER.parse_args()
# Assign global variables
try:
# Set log level — --verbose overrides --loglevel.
# The root logger level must be lowered so that DEBUG messages from
# all sub-modules (client, converter, …) are not silently dropped
# before they reach any handler.
if ARGS.verbose:
log_level = logging.DEBUG
else:
log_level = getattr(logging, ARGS.loglevel.upper(), logging.INFO)
logging.getLogger().setLevel(log_level)
LOGGER.setLevel(log_level)
MARKDOWN_FILES = expand_file_globs(ARGS.markdownFile, ARGS.exclude_patterns)
SPACE_KEY = ARGS.spacekey
USERNAME = os.getenv("CONFLUENCE_USERNAME", ARGS.username)
API_KEY = os.getenv("CONFLUENCE_API_KEY", ARGS.apikey)
ORGNAME = os.getenv("CONFLUENCE_ORGNAME", ARGS.orgname)
ANCESTOR = ARGS.ancestor
NOSSL = ARGS.nossl
DELETE = ARGS.delete
SIMULATE = ARGS.simulate
VERSION = ARGS.version
MARKDOWN_SOURCE = ARGS.markdownsrc
LABELS = ARGS.labels
PROPERTIES = dict(ARGS.properties)
ATTACHMENTS = ARGS.attachment
CONTENTS = ARGS.contents
TITLE = ARGS.title
REMOVE_EMOJIES = ARGS.remove_emojies
RENDER_MERMAID = ARGS.render_mermaid
validate_args(USERNAME, API_KEY, MARKDOWN_FILES, ORGNAME)
except Exception as err:
LOGGER.error("\n\nException caught:\n%s ", err)
LOGGER.error("\nFailed to process command line arguments. Exiting.")
sys.exit(1)
LOGGER.info("\t----------------------------------")
LOGGER.info("\tMarkdown to Confluence Upload Tool")
LOGGER.info("\t----------------------------------")
LOGGER.info("Files to process:\t%d", len(MARKDOWN_FILES))
LOGGER.info("Space Key:\t%s", SPACE_KEY)
if ANCESTOR:
LOGGER.info("Ancestor:\t%s", ANCESTOR)
LOGGER.debug("Org/URL:\t%s", ORGNAME)
LOGGER.debug("Username:\t%s", USERNAME)
if ARGS.verbose:
LOGGER.debug("Verbose logging is enabled (DEBUG level).")
multi_file = len(MARKDOWN_FILES) > 1
# ── Build ConfluenceConverter instances (one per file) ─────────────────
converters = []
for md_file in MARKDOWN_FILES:
LOGGER.info("Processing:\t%s", md_file)
cc = ConfluenceConverter(
md_file,
MARKDOWN_SOURCE,
TITLE,
ORGNAME,
not NOSSL,
USERNAME,
SPACE_KEY,
API_KEY,
ANCESTOR,
VERSION,
)
converters.append((md_file, cc))
# ── Pass 1: publish all pages, collect page_map ──────────────────────
# When multi_file is True we run a first pass without cross-file link
# resolution so that ALL page IDs are known before the second pass.
page_map: dict = {} # abs_path → {"page_id": int, "title": str, "url": str}
for md_file, cc in converters:
LOGGER.info("Pass 1 — publishing:\t%s", md_file)
result = cc.convert(
SIMULATE,
DELETE,
REMOVE_EMOJIES,
CONTENTS,
LABELS,
PROPERTIES,
ATTACHMENTS,
render_mermaid=RENDER_MERMAID,
page_map=None, # no cross-file resolution yet
)
if result is not None:
page_map[os.path.abspath(md_file)] = result
# ── Pass 2 (multi-file only): re-update pages with cross-file links ───
if multi_file and page_map and not SIMULATE and not DELETE:
LOGGER.info(
"Pass 2 — resolving cross-file links across %d pages …",
len(page_map),
)
for md_file, cc in converters:
LOGGER.info("Pass 2 — updating:\t%s", md_file)
cc.convert(
SIMULATE,
DELETE,
REMOVE_EMOJIES,
CONTENTS,
LABELS,
PROPERTIES,
ATTACHMENTS,
render_mermaid=RENDER_MERMAID,
page_map=page_map,
)
|