antix19
authorMichael Wagner <michael@wagnertech.de>
Fri, 26 Mar 2021 18:31:44 +0000 (19:31 +0100)
committerMichael Wagner <michael@wagnertech.de>
Fri, 26 Mar 2021 18:31:44 +0000 (19:31 +0100)
doc/README
python/mDoc/mdoc/extractor.py [new file with mode: 0644]
python/mDoc/mdoc_extract.py [new file with mode: 0755]

index fb03237..a1a42dc 100644 (file)
@@ -27,4 +27,8 @@ Dabei gelten folgende Regeln:
  x Input: Sie können direkt auf die Variablen value (der Wert des tags) und source (das XML-
    Dokument) zugreifen.
  x Output: Der Wert der Variablen out wird in die Zieldatei kopiert
+- mDoc-spezifische Tags (Docbook-Erweiterung)
+ x <line>: Einzelne Zeilen innerhalb eines Paragraphen
+ x <item>: Definition von Testbausteinen, wenn im Paragraphen auch Hervorhebungs-Tags (wie 
+   <emphasis>) vorkommen.
+
diff --git a/python/mDoc/mdoc/extractor.py b/python/mDoc/mdoc/extractor.py
new file mode 100644 (file)
index 0000000..1b9b7be
--- /dev/null
@@ -0,0 +1,65 @@
+'''
+Created on 26.03.2021
+
+@author: antix19
+'''
+from mutil.XmlExtractor import XmlExtractor
+from mutil import XmlExtractor as XMLE
+from pickle import NONE, FALSE, TRUE
+
+def eval_class(classs, item):
+    item_hira = item.split(".")
+    ilen = len(item_hira) 
+    class_hira = classs.split(".")
+    if len(class_hira) < len(item_hira):
+        return False,ilen
+    for i in range(len(item_hira)):
+        if item_hira[i] != class_hira[i]:
+            return False,ilen
+    return True,ilen
+    
+def is_for_print(classs, attrs):
+    include = False
+    exclude = False
+    ilen    = 0
+    elen    = 0
+    if "include" in attrs:
+        include,ilen = eval_class(classs, attrs["include"])
+    if "exclude" in attrs:
+        exclude,elen = eval_class(classs, attrs["exclude"])
+    if ilen > 0 and elen == 0:
+        return include
+    elif elen > 0 and ilen == 0:
+        return not exclude
+    elif ilen > 0 and elen > 0:
+        if ilen > elen:
+            if include:
+                return True
+            else:
+                return not exclude
+        elif elen > ilen:
+            if exclude:
+                return False
+            else:
+                return include
+    return True
+    
+    
+def extract(file, classs, target):
+    source = XmlExtractor()
+    source.openInput(file)
+    
+    # open output file
+    out_file = open(target, "w")
+    
+    source.requireChild("document")
+    (ctl,elem, value, attrs) = source.extractElement(XMLE.EC_BEG)
+    while ctl != XMLE.EC_END:
+        if elem != "text":
+            raise EnvironmentError("<text> node expected")
+        if is_for_print(classs, attrs):
+            out_file.write(value+"\n")
+        (ctl,elem, value, attrs) = source.extractElement(ctl)
+    return 0
+
+        
\ No newline at end of file
diff --git a/python/mDoc/mdoc_extract.py b/python/mDoc/mdoc_extract.py
new file mode 100755 (executable)
index 0000000..6226d79
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/python3
+'''
+Created on 26.03.2021
+
+@author: antix19
+'''
+import sys
+from mdoc import extractor
+import re
+
+usage = 'mdoc-extract <source> <class>'
+
+def command_dispatch(args):
+    
+    # check runtime parameters
+    if len(args) < 2:
+        raise RuntimeError("mdoc-extract needs 2 arguments.")
+    
+    file = args[0]
+    classs = args[1]
+    target = re.sub(r'\..*', '.tex', file)
+    
+    return extractor.extract(file, classs, target)
+    
+
+# evaulate runtime parameters
+if __name__ == '__main__':
+    try:
+        ret = command_dispatch(sys.argv[1:])
+        if ret != 0:
+            print ("Operation returned: "+str(ret)) 
+        exit (ret)
+    except Exception as e:
+        print (e)
+        print (usage)
+        raise
\ No newline at end of file