anchor.diff:  { fetchurl, lib, stdenv, python3
   , fetchFromGitHub, autoreconfHook
  +, installShellFiles
   , enableStandardFeatures ? false
   , sourceHighlight ? null
   , highlight ? null
  
positive.code:         -e "s|^W3M =.*|W3M = '${w3m}/bin/w3m'|" \
          -e "s|^LYNX =.*|LYNX = '${lynx}/bin/lynx'|" \
          -e "s|^XMLLINT =.*|XMLLINT = '${libxml2.bin}/bin/xmllint'|" \
          -e "s|^EPUBCHECK =.*|EPUBCHECK = 'nixpkgs_is_missing_epubcheck'|" \
          -i a2x.py
    '' else ''
      sed -e "s|^ENV =.*|ENV = dict(XML_CATALOG_FILES='${docbook_xml_dtd_45}/xml/dtd/docbook/catalog.xml ${docbook_xsl_ns}/xml/xsl/docbook/catalog.xml ${docbook_xsl}/xml/xsl/docbook/catalog.xml')|" \
          -e "s|^XSLTPROC =.*|XSLTPROC = '${libxslt.bin}/bin/xsltproc'|" \
          -e "s|^XMLLINT =.*|XMLLINT = '${libxml2.bin}/bin/xmllint'|" \
          -i a2x.py
    '') + ''
      patchShebangs --host \
        asciidoc.py \
        a2x.py \
        tests/testasciidoc.py \
        filters/code/code-filter.py \
        filters/latex/latex2img.py \
        filters/music/music2png.py \
        filters/unwraplatex.py \
        filters/graphviz/graphviz2png.py
  
      # Hardcode the path to its own asciidoc.
      # This helps with cross-compilation.
      substituteInPlace a2x.py \
        --replace "find_executable(ASCIIDOC)" "'${placeholder "out"}/bin/asciidoc'"
  
      # Note: this substitution will not work in the planned 10.0.0 release:
      #
      # https://github.com/asciidoc/asciidoc-py3/commit/dfffda23381014481cd13e8e9d8f131e1f93f08a
      #
      # Update this substitution to:
      #
      # --replace "python3 -m asciidoc.a2x" "python3 -m asciidoc.a2x -a revdate=01/01/1980"
      substituteInPlace Makefile.in \
        --replace "python3 a2x.py" "python3 a2x.py -a revdate=01/01/1980"
  
      # Fix tests
      for f in $(grep -R --files-with-matches "2002-11-25") ; do
        substituteInPlace $f --replace "2002-11-25" "1970-01-01"
        substituteInPlace $f --replace "00:37:42" "00:00:01"
      done
    '' + lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) ''
      # We want to use asciidoc from the build platform to build the documentation.
      substituteInPlace Makefile.in \
        --replace "python3 a2x.py" "python3 ${buildPackages.asciidoc}/bin/a2x.py"
    '';
  
    preInstall = "mkdir -p $out/etc/vim";
    makeFlags = lib.optional stdenv.isCygwin "DESTDIR=/.";
  
    checkInputs = [ sourceHighlight ];
    doCheck = true;
  
    meta = with lib; {
      description = "Text-based document generation system";
  
negative.code:   docbook_xsl_ns != null &&
    docbook_xsl != null &&
    (fop != null || !enableJava) &&
    epubcheck != null &&
    gnused != null &&
    coreutils != null;
  
  