Skip to main content

A Markdown extension that makes it easy to reference Javadocs

Project description

PyPI version

🐍 Python Markdown Javadoc References

Have you ever been tired of copying entire Javadoc URLs into your Markdown just to link to the documentation for some Java classes? That is where is extension comes in handy!

This python markdown extension adds the ability to directly reference Java classes, methods and fields in your markdown to link to their documentation. It supports both Javadoc prior to Java 9 and from Java 9 onwards.

Installation

You can find the extension on PyPi under markdown_javadoc_references.

With plain python markdown

To use this extension with the API of python markdown, just add JavaDocRefExtension to the list of your extensions and provide the URLs to use.

import markdown
from markdown_javadoc_references import JavaDocRefExtension

urls = [
    'https://docs.oracle.com/en/java/javase/24/docs/api/',
    {
        'alias': 'jdk8',
        'url': 'https://docs.oracle.com/javase/8/docs/api/'
    }
]

text = 'your markdown text with reference [String][java.lang.String]'
result = markdown.markdown(text, extensions=[JavaDocRefExtension(urls=urls)])

With MkDocs

To use this extension with MkDocs just add it to your mkdocs.yml:

markdown_extensions:
  - markdown_javadoc_references:
      - urls:
          - 'https://docs.oracle.com/en/java/javase/24/docs/api/'
          - alias: 'jdk8'
            url: 'https://docs.oracle.com/javase/8/docs/api/'

Usage

Referencing java methods, classes or fields is similar to how it is done in normal (markdown) javadoc comments, with the slight change of double [ and ] in the second part of the reference. For example [String#concat(String)][[String#concat(String)]] will result in String#concat(String)

Autolinks

Often times the text presented to the user can be easily derived from the used reference. For this common case you can use the autolink syntax to avoid writing it twice.

<String#concat(String)> is the same as [String#concat(String)][[String#concat(String)]]

Formatting

By default, the text that is presented to the user is the reference with the packages stripped.

For example:

  • <java.lang.String> -> [String][[java.lang.String]])
  • <java.lang.String#concat(java.lang.String)> -> [String#concat(String)][[java.lang.String#concat(java.lang.String)]]
  • <java.lang.String#CASE_INSENSITIVE_ORDER -> [String#CASE_INSENSITIVE_ORDER][[java.lang.String#CASE_INSENSITIVE_ORDER]]

To configure how the text is derived, you can provide a small python script via the config key autolink-format. In this environment, you can use the ref variable to get the resolved reference entity. Now you can just use a match construct to define the specific formatting for each entity type.

The provided code then should just return the string presented to the user or an xml.etree.ElementTree.Element instance. The (class) names Klass, Field, Method, Type and etree (which is xml.etree.ElementTree) are automatically imported for you. Please take a look at the source code to learn more about the data and utility functions of each entity. You need some basic python programming skills to do this (or just ask ChatGPT).

[!NOTE] The content of this config option will be copied in as the body of a python function.

Example (default formatter):

match ref:
    case Klass():
        return f"@{ref.name}" if ref.type == Type.ANN_INTERFACE else ref.name
    case Field():
        return f'{ref.klass.name}#{ref.name}'
    case Method():
        return f'{ref.klass.name}#{ref.name}({ref.parameter_names_joined()})'

Example (default formatter with reference texts as code blocks; etree):

name = None

match ref:
    case Klass():
        name = f"@{ref.name}" if ref.type == Type.ANN_INTERFACE else ref.name
    case Field():
        name = f'{ref.klass.name}#{ref.name}'
    case Method():
        name = f'{ref.klass.name}#{ref.name}({ref.parameter_names_joined()})'
        
code = etree.Element('code')
code.text = name
return code

Or in the mkdocs.yml:

markdown_extensions:
  - markdown_javadoc_references:
      - urls:
          - 'https://docs.oracle.com/en/java/javase/24/docs/api/'
      - autolink-format: |
          match ref:
            case Klass():
              return ref.name
            case Field():
              return f'{ref.klass.name}#{ref.name}'
            case Method():
              return f'{ref.klass.name}#{ref.name}({ref.parameter_names_joined()})'

Classes

To just reference a class you only need to provide its name: <String>. If multiple classes with this name exists, just add a package or url alias.

Methods

To reference methods you provide the classname and method name following its parameters separated by #: <String#conact(String).

Again, if multiple javadocs are matched just add a url alias or packages to parameters or the enclosing class.

Packages

To clarify which class to use, you can add a package in front of it: <java.lang.String>.

Furthermore, you can also a package to method parameters: <String#concact(java.lang.String)

[!WARNING] If multiple matches are found for a reference, the reference will be marked as "Invalid"!

Fields

Like methods, fields can be referred to in a similar style <String#CASE_INSENSITIVE_ORDER> will link to String#CASE_INSENSITIVE_ORDER

Constructors

To refer to constructors, just add <init> in the place where the method name would be: String#<init>(byte[],int,int) will link to String#(byte[],int,int)

URL aliases

Instead of stating the package explicitly, you can also add a URL alias to your reference. For that to work, you have to state the alias for your javadoc site in your configuration. (take a look at installation)

Assuming you have a javadoc site configured with the alias "jdk8":

markdown_extensions:
  - markdown_javadoc_references:
      - urls:
        - 'https://docs.oracle.com/en/java/javase/24/docs/api/'
        - alias: 'jdk8'
          url: 'https://docs.oracle.com/javase/8/docs/api/'

you can now use this alias to force the extension to only search under the site https://docs.oracle.com/javase/8/docs/api/ for the referred to javadoc: <jdk8 -> String#<init>(byte[],int,int)>

Additionally, if you don't have an alias configured explicitly, you can still use the whole URL: <https://docs.oracle.com/en/java/javase/24/docs/api/ -> String#<init>(byte[],int,int)>

[!IMPORTANT] The URL has to be still mentioned in the configuration!

Sites not automatically searched

In certain cases, it is only necessary to reference classes from a documentation page a few times. However, adding these pages normally would lead to a whole series of conflicts. Therefore, you can set the auto_searched option to false for such classes. (You must give them an alias!)

markdown_extensions:
  - markdown_javadoc_references:
      - urls:
        - alias: 'jdk8'
          url: 'https://docs.oracle.com/javase/8/docs/api/'
          auto_searched: 'false'

Then you can just reference them with help of aliases: <jdk8 -> String>.

Explicitly setting type of javadoc site

In most cases, the extension will find out if a docsite must be loaded as prior to Java 9 or as from Java 9 onwards. But in some cases this isn't always possible.

In such cases you have to state the needed type of javadoc site explicitly:

  • 'old' for prior java 9 site (java 1 to 8)
  • 'new' for java 9 onwards (java 9, 10+)
markdown_extensions:
  - markdown_javadoc_references:
      - urls:
          - url: 'https://docs.oracle.com/javase/8/docs/api/'
            type: 'old'

Javadoc.io special handling

Many projects host their javadocs on https://javadoc.io, which is a convenient way to find and host javadocs for multiple versions of a project.

Unfortunately, the handling of subpages and the provided html code of this website is somehow different from normal javadoc. Thus, this extension provides explicit support for:

If the automatic mechanism shouldn't work one day, just put the static sites, e.g. https://javadoc.io/static/com.zaxxer/HikariCP/7.0.1/ in your config.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_javadoc_references-0.5.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_javadoc_references-0.5.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file markdown_javadoc_references-0.5.1.tar.gz.

File metadata

  • Download URL: markdown_javadoc_references-0.5.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Linux/6.12.55

File hashes

Hashes for markdown_javadoc_references-0.5.1.tar.gz
Algorithm Hash digest
SHA256 0ae97b8f014e041a319691ea1af3304d6a6f70c83e6db03fb01045e90d1fb5aa
MD5 e1c0d0f852b1d208cf96af2663b78fcc
BLAKE2b-256 9baf41457b6ed96b9083e7b855d097b02298bae8a817d4ceb1c0b5d550102a42

See more details on using hashes here.

File details

Details for the file markdown_javadoc_references-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_javadoc_references-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ccddca80af0262618f664a796b8b3e97bd98cfbe43fcefb967e39bff4510c76a
MD5 b95871e72cc147410e8e0be8f055bcd5
BLAKE2b-256 1dab6bb100ce73beabc15738b553861ce47289f36354446c3ddf70cbcc191ee5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page