code: def get_out_filename(
      output_path,
      filename,
      ext,
      kind,
      dirpage,
      dircat,
      categories,
      wp_custpost,
      slug_subs,
  ):
      filename = os.path.basename(filename)
  
      # Enforce filename restrictions for various filesystems at once; see
      # https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
      # we do not need to filter words because an extension will be appended
      filename = re.sub(
          r'[<>:"/\\|?*^% ]', "-", filename
      )  # invalid chars
      filename = filename.lstrip(".")  # should not start with a dot
      if not filename:
          filename = "_"
      filename = filename[:249]  # allow for 5 extra characters
  
      out_filename = os.path.join(output_path, filename + ext)
      # option to put page posts in pages/ subdirectory
      if dirpage and kind == "page":
          pages_dir = os.path.join(output_path, "pages")
          if not os.path.isdir(pages_dir):
              os.mkdir(pages_dir)
          out_filename = os.path.join(pages_dir, filename + ext)
      elif not dirpage and kind == "page":
          pass
      # option to put wp custom post types in directories with post type
      # names. Custom post types can also have categories so option to
      # create subdirectories with category names
      elif kind != "article":
          if wp_custpost:
              typename = slugify(kind, regex_subs=slug_subs)
          else:
              typename = ""
              kind = "article"
          if dircat and (len(categories) > 0):
              catname = slugify(
                  categories[0],
                  regex_subs=slug_subs,
                  preserve_case=True,
              )
          else:
              catname = ""
          out_filename = os.path.join(
              output_path, typename, catname, filename + ext
          )
          if not os.path.isdir(
              os.path.join(output_path, typename, catname)
          ):
              os.makedirs(os.path.join(output_path, typename, catname))
      # option to put files in directories with categories names
      elif dircat and (len(categories) > 0):
          catname = slugify(
              categories[0], regex_subs=slug_subs, preserve_case=True
          )
          out_filename = os.path.join(
              output_path, catname, filename + ext
          )
          if not os.path.isdir(os.path.join(output_path, catname)):
              os.mkdir(os.path.join(output_path, catname))
  
      return out_filename
  
imports: import argparse
  import datetime
  import logging
  import os
  import re
  import subprocess
  import sys
  import tempfile
  import time
  from collections import defaultdict
  from html import unescape
  from urllib.error import URLError
  from urllib.parse import quote, urlparse, urlsplit, urlunsplit
  from urllib.request import urlretrieve
  from pelican.log import init
  from pelican.settings import DEFAULT_CONFIG
  from pelican.utils import SafeDatetime, slugify
label_desc: Use image node_id 14 for calling a global function with example usage: get_out_filename(output_path, filename, ext, kind, dirpage, dircat, categories, wp_custpost, slug_subs) and returns: out_filename