diff --git a/pydown/pydown.py b/pydown/pydown.py index f2c41637..82b40527 100755 --- a/pydown/pydown.py +++ b/pydown/pydown.py @@ -28,23 +28,91 @@ def HtmlEscape(text): text = text.replace('>', '>') return text -def MdDocString(doc): - lines = doc.split('\n') - md = '' - mode = '' - for line in lines: - if re.match(r'^\-+$', line): - continue - if line in ['Parameters', 'Returns', 'Example', 'Properties']: - mode = line - continue - if line.strip() == '': - mode = '' +def SymbolLink(name): + if 'a' <= name[0] <= 'z': + # Assume built-in Python identifier, so do not link + return '`{0}`'.format(name) + # [`astro_time_t`](#astro_time_t) + return '[`{0}`](#{0})'.format(name) + +class ParmInfo: + def __init__(self, name, type): + self.name = name + self.type = type + self.description = '' + + def AppendDescriptionLine(self, line): + self.description += line.strip() + ' ' + +class DocInfo: + def __init__(self, doc): + self.description = '' + self.parameters = [] + self.attributes = [] + + lines = doc.split('\n') + + # First line is boldfaced if followed by blank line. + if len(lines) >= 2 and lines[0].strip() != '' and lines[1].strip() == '': + self.summary = lines[0] + lines = lines[2:] + else: + self.summary = '' + + currentAttr = None + currentParm = None + mode = '' + for line in lines: + if re.match(r'^\-+$', line): + continue + if line in ['Parameters', 'Returns', 'Example', 'Examples', 'Properties']: + mode = line + continue + if line.strip() == '': + mode = '' + continue + if mode == 'Parameters': + currentParm = self.ProcessParmAttrLine(line, currentParm, self.parameters) + elif mode == 'Properties': + currentAttr = self.ProcessParmAttrLine(line, currentAttr, self.attributes) + elif mode == 'Returns': + pass + elif mode == 'Example' or mode == 'Examples': + pass + + def ProcessParmAttrLine(self, line, item, itemlist): + if line.startswith(' '): + # The first line of description, or another line of description. + item.AppendDescriptionLine(line) + else: + # name : type + token = line.split(':') + if len(token) != 2: + raise Exception('Expected name:type but found: "{}"'.format(line)) + item = ParmInfo(token[0].strip(), token[1].strip()) + itemlist.append(item) + return item + + def Table(self, itemlist, tag): + md = '' + if itemlist: + md += '| Type | {} | Description |\n'.format(tag) + md += '| --- | --- | --- |\n' + for p in itemlist: + md += '| {} | {} | {} |\n'.format(SymbolLink(p.type), '`' + p.name + '`', p.description.strip()) md += '\n' - continue - text = HtmlEscape(line) - md += text + '\n' - return md + return md + + def Markdown(self): + md = '\n' + if self.summary: + md += '**' + self.summary + '**\n\n' + if self.description: + md += self.description + '\n\n' + md += self.Table(self.parameters, 'Parameter') + md += self.Table(self.attributes, 'Attribute') + md += '\n' + return md def MdSignature(sig): text = str(sig) @@ -61,7 +129,22 @@ def MdFunction(func): md += '\n' md += '\n'.format(func.__name__) md += '### ' + func.__name__ + MdSignature(sig) + '\n' - md += MdDocString(doc) + '\n' + info = DocInfo(doc) + md += info.Markdown() + md += '\n' + return md + +def MdClass(c): + md = '' + doc = inspect.getdoc(c) + if doc: + md += '\n' + md += '---\n' + md += '\n' + md += '\n'.format(c.__name__) + md += '### class ' + c.__name__ + '\n' + info = DocInfo(doc) + md += info.Markdown() md += '\n' return md @@ -86,6 +169,9 @@ def Markdown(module): md += '## Functions\n' md += '\n' + for c in classlist: + md += MdClass(c) + for func in funclist: md += MdFunction(func) diff --git a/source/python/README.md b/source/python/README.md index 1c23f058..773fd984 100644 --- a/source/python/README.md +++ b/source/python/README.md @@ -4,47 +4,143 @@ ## Functions +--- + + +### class ApsisKind + + + + +--- + + +### class BadVectorError + + + + +--- + + +### class Body + + + + +--- + + +### class Direction + + + + +--- + + +### class EarthNotAllowedError + + + + +--- + + +### class Error + + + + +--- + + +### class IntEnum + + + + +--- + + +### class InternalError + + + + +--- + + +### class InvalidBodyError + + + + +--- + + +### class NoConvergeError + + + + +--- + + +### class Observer + +**Represents the geographic location of an observer on the surface of the Earth.** + + + + +--- + + +### class Refraction + + + + +--- + + +### class Time + +**Represents a date and time used for performing astronomy calculations.** + +| Type | Parameter | Description | +| --- | --- | --- | +| `float` | `ut` | UT1/UTC number of days since noon on January 1, 2000. See the `ut` attribute of this class for more details. | + + + + --- ### BodyCode(name) -Finds the Body enumeration value, given the name of a body. -name: str - The common English name of a supported celestial body. +**Finds the Body enumeration value, given the name of a body.** -Body - If `name` is a valid body name, returns the enumeration - value associated with that body. - Otherwise, returns `Body.Invalid`. +| Type | Parameter | Description | +| --- | --- | --- | +| `str` | `name` | The common English name of a supported celestial body. | ->>> astronomy.BodyCode('Mars') -<Body.Mars: 3> - --- ### GeoMoon(time) -Calculates the geocentric position of the Moon at a given time. -Given a time of observation, calculates the Moon's position as a vector. -The vector gives the location of the Moon's center relative to the Earth's center -with x-, y-, and z-components measured in astronomical units. +**Calculates the geocentric position of the Moon at a given time.** -This algorithm is based on Nautical Almanac Office's *Improved Lunar Ephemeris* of 1954, -which in turn derives from E. W. Brown's lunar theories from the early twentieth century. -It is adapted from Turbo Pascal code from the book -[Astronomy on the Personal Computer](https://www.springer.com/us/book/9783540672210) -by Montenbruck and Pfleger. +| Type | Parameter | Description | +| --- | --- | --- | +| [`Time`](#Time) | `time` | The date and time for which to calculate the Moon's position. | -time : Time - The date and time for which to calculate the Moon's position. - -Vector - The Moon's position as a vector in J2000 Cartesian equatorial coordinates. @@ -52,6 +148,6 @@ Vector ### unique(enumeration) -Class decorator for enumerations ensuring unique member values. +