Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions antiweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,21 @@

Read the documentation of the corresponding event file handler (:ref:`FileChangeHandler <label-filechangehandler>`).

.. _label-supported_languages:

*******************
Supported Languages
*******************

#@include(supported_languages doc, antiweb_lib\readers\config.py)

.. _label-add_language:

************************
How to add new languages
************************

New languages are added by writing a new Reader class
and registering it in the readers dictionary (see readers).
A simple Reader example is provides by :py:class:`CReader`
a more advanced reader is :py:class:`PythonReader`.

#@include(comments doc, antiweb_lib\readers\config.py)
#@include(get_comment_markers doc, antiweb_lib\document.py)
#@include(new_language doc, antiweb_lib\readers\config.py)

*******
Example
Expand Down Expand Up @@ -233,17 +236,21 @@
from watchdog.observers import Observer
from antiweb_lib.filechangehandler import FileChangeHandler

from antiweb_lib.readers.config import is_file_supported

#@rstart(management)

#<<management>>
#==============

#@code

__version__ = "0.9.1"

logger = logging.getLogger('antiweb')

def sys_exit(message):
logger.error(message)
sys.exit(1)

#@cstart(parsing)
def parsing():
#@start(parsing doc)
Expand Down Expand Up @@ -282,7 +289,7 @@ def parsing():
if not args:
args.append(os.getcwd())
# parsing() returns the selected options, arguments (the filepath/folderpath) and the parser
return (options, args, parser)
return options, args, parser
#@(parsing)

def main():
Expand Down Expand Up @@ -323,8 +330,7 @@ def main():
#The program aborts if the directory does not exist or if the path refers to a file.
#A file is not allowed here because the -r option requires a directory.
if not os.path.isdir(directory):
logger.error("directory not found: %s", directory)
sys.exit(1)
sys_exit("directory not found: %s" % directory)

os.chdir(directory)

Expand All @@ -335,22 +341,18 @@ def main():

#@code

#Only files with the following extensions will be processed
rst_extension = ".rst"
ext_tuple = (".cs",".cpp",".py",".cc", rst_extension, ".xml")

handled_files = []

for root, dirs, files in os.walk(directory, topdown=False):
for filename in files:
fname = os.path.join(root, filename)

if not (os.path.isfile(fname) and fname.endswith(ext_tuple)):
if not (os.path.isfile(fname) and is_file_supported(fname)):
continue

# rst files should be handled last as they might be a documentation file of a
# file that is not yet processed -> in this case the rst file will be ignored
if fname.endswith(rst_extension):
if fname.endswith(".rst"):
handled_files.append(fname)
else:
handled_files.insert(0, fname)
Expand Down Expand Up @@ -381,7 +383,7 @@ def main():
try:
#observed directory => input directory
#recursive option is true in order to monitor all subdirectories
observer.schedule(FileChangeHandler(directory, ext_tuple, options, created_files), path=directory, recursive=True)
observer.schedule(FileChangeHandler(directory, options, created_files), path=directory, recursive=True)

print("\n------- starting daemon mode (exit with enter or ctrl+c) -------\n")

Expand Down Expand Up @@ -409,8 +411,10 @@ def main():
#The program aborts if the file does not exist or if the path refers to a directory.
#A directory is not allowed here because a directory can only be used with the -r option.
if not os.path.isfile(absolute_file_path):
logger.error("file not found: %s", absolute_file_path)
sys.exit(1)
sys_exit("file not found: %s" % absolute_file_path)

if not is_file_supported(absolute_file_path):
sys_exit("file is not supported: %s" % absolute_file_path)

directory = os.path.split(absolute_file_path)[0]

Expand Down
40 changes: 3 additions & 37 deletions antiweb_lib/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,10 @@
import os
import operator
import logging
import pygments.lexers as pm

from antiweb_lib.readers.config import readers, comments

from antiweb_lib.readers.Line import Line

from antiweb_lib.readers.Reader import Reader
from antiweb_lib.readers.CReader import CReader
from antiweb_lib.readers.CSharpReader import CSharpReader
from antiweb_lib.readers.ClojureReader import ClojureReader
from antiweb_lib.readers.GenericReader import GenericReader
from antiweb_lib.readers.RstReader import RstReader
from antiweb_lib.readers.PythonReader import PythonReader
from antiweb_lib.readers.config import get_reader_for_file


logger = logging.getLogger('antiweb')
Expand Down Expand Up @@ -266,9 +257,7 @@ def insert_macros(subdoc):

else:
#parse the file
lexer = pm.get_lexer_for_filename(rpath)
single_comment_markers, block_comment_markers = get_comment_markers(lexer.name)
reader = readers.get(lexer.name, Reader)(lexer, single_comment_markers, block_comment_markers)
reader = get_reader_for_file(fpath)

doc = Document(text, reader, rpath, self.tokens)
doc.collect_blocks()
Expand Down Expand Up @@ -388,27 +377,4 @@ def find_next_directive(block):
return block
#@edoc
#@rinclude(find_next_directive)
#@(Document.compile_block)

#@cstart(get_comment_markers)

def get_comment_markers(lexer_name):
#@start(get_comment_markers doc)
#From the map above the comment markers are retrieved via the following method:

"""
.. py:function:: get_comment_markers(lexer_name)

Retrieves the language specific comment markers from the comments map.
The comment markers of C serves as the default comment markers if the lexer name cannot be found.

:param string lexer_name: The name of the pygments lexer.
:return: The single and comment block markers defined by the language
"""
#@indent 4
#@include(get_comment_markers)
#@(get_comment_markers doc)
comment_markers = comments.get(lexer_name, comments["C"])
single_comment_markers = comment_markers[0]
block_comment_markers = comment_markers[1]
return single_comment_markers, block_comment_markers
#@(Document.compile_block)
7 changes: 3 additions & 4 deletions antiweb_lib/filechangehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from watchdog.events import FileSystemEventHandler
from antiweb_lib.write import write
from antiweb_lib.write import create_write_string
from antiweb_lib.readers.config import is_file_supported
import time

#@start()
Expand Down Expand Up @@ -40,7 +41,6 @@ class FileChangeHandler(FileSystemEventHandler):
This handler is responsible for handling changed file events in antiweb's daemon mode.

:param string directory: absolute path to the monitored source directory
:param tuple<string> extensions: contains all handled file extensions ("*.cs", "*.py", etc)
:param options: antiweb commandline options
:param created_files: a set which contains the absolute paths of all previously created documentation files

Expand All @@ -49,11 +49,10 @@ class FileChangeHandler(FileSystemEventHandler):

#@(FileChangeHandler doc)

def __init__(self, directory, extensions, options, created_files):
def __init__(self, directory, options, created_files):
self._directory = directory
#antiweb commandline options
self._options = options
self._handled_extensions = extensions
self._event_counter = 0
self._created_files = set()
self._created_files.update(created_files)
Expand Down Expand Up @@ -87,7 +86,7 @@ def process_event(self, event):
#the file has been moved so it is now located in event.dest_path
changed_file = event.dest_path

ignore_change = changed_file in self._created_files or not changed_file.endswith(self._handled_extensions) or \
ignore_change = changed_file in self._created_files or not is_file_supported(changed_file) or \
event.is_directory or event.event_type == "deleted"

if not ignore_change:
Expand Down
2 changes: 1 addition & 1 deletion antiweb_lib/readers/ClojureReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ def filter_output(self, lines):
#remove comments but not chapters
l.text = l.indented(stext[1:])

yield l
yield l
#@edoc
#@(ClojureReader)
76 changes: 76 additions & 0 deletions antiweb_lib/readers/Language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
__author__ = "Michael Reithinger, Philipp Rathmanner, Lukas Tanner, Philipp Grandits, and Christian Eitner"
__copyright__ = "Copyright 2017, antiweb team"
__license__ = "GPL"
__version__ = "0.9.1"
__maintainer__ = "antiweb team"
__email__ = "antiweb@freelists.org"

import sys
import pygments.lexers as pm
from pygments.util import ClassNotFound

import logging

logger = logging.getLogger('antiweb')

"""
@start()
.. _label-language:

@include(Language doc)
"""


#@cstart(Language)
class Language(object):
#@start(Language doc)
#Language
#========
"""
.. py:class:: Language(name, reader, single_comments, block_comments)

This class represents a supported language of antiweb.
"""

#@include(Language)
#@include(Language.__init__ doc)
#@include(Language.get_reader doc)
#@(Language doc)


#@cstart(Language.__init__)
def __init__(self, name, reader, single_comments, block_comments):
"""
.. py:method:: __init__(name, reader, single_comments, block_comments)

The constructor.
The comment markers of a language have to be defined in the format:
``[single_comment_tokens]`` and ``[start_block_token, end_block_token]``.
Multiple single and block comment markers can be defined.

:param string name: the name of a pygments lexer.
:param class reader: the reader class that should be used for the corresponding language.
:param list single_comments: a list of single comment characters supported by the language (e.g. ['#']).
:param list<tuple> block_comments: a list of block comment character tuples supported by the language (e.g. ["/*","*/"]).
"""
try:
self.lexer = pm.get_lexer_by_name(name)
except ClassNotFound:
logger.error("\nError: No lexer for alias: '%s' found", name)
sys.exit(1)

self.reader = reader
self.single_comments = single_comments
self.block_comments = block_comments

#the lexer filenames have the format: ['*.cs', '*.cpp', ..]
self.supported_files = self.lexer.filenames

#@cstart(Language.get_reader)
def get_reader(self):
"""
.. py:method:: get_reader()

returns a new instance of the :py:attr:`reader` class.
"""
return self.reader(self.lexer, self.single_comments, self.block_comments)
1 change: 0 additions & 1 deletion antiweb_lib/readers/Reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
@include(GenericReader doc, GenericReader.py)
@include(RstReader doc, RstReader.py)
@include(XmlReader doc, XmlReader.py)
@include(reader_dictionary doc, config.py)
"""
#@rstart(readers)
#.. _readers:
Expand Down
Loading