1
2
3
4
5 """
6
7 Serializer for N3 (Notation 3)
8 ==============================
9
10 This is a plugin for formatting DOAP output as N3 (Notation 3) syntax.
11
12 """
13
14 __docformat__ = 'epytext'
15
16 import logging
17 from cStringIO import StringIO
18
19 from rdflib import ConjunctiveGraph
20
21 from doapfiend.plugins.base import Plugin
22
23 LOG = logging.getLogger(__name__)
24
25
26 -def get_n3(xml_text, color=False):
27 '''
28 Return N3 (Notation 3) text
29
30 @param xml_text: XML/RDF
31 @type xml_text: string
32
33 @rtype: unicode
34 @return: DOAP in Notation 3
35 '''
36 store = ConjunctiveGraph()
37 graph = store.parse(StringIO(xml_text), publicID=None, format="xml")
38 notation3 = graph.serialize(format="n3")
39
40 if color:
41
42
43 try:
44 from pygments import highlight
45 from doapfiend.lexers import Notation3Lexer
46 from pygments.formatters import TerminalFormatter
47 except ImportError:
48 return notation3
49 return highlight(notation3,
50 Notation3Lexer(),
51 TerminalFormatter(full=False))
52 else:
53 return notation3
54
56
57 """Class for formatting DOAP output"""
58
59
60 name = "n3"
61 enabled = False
62 enable_opt = None
63
68
70 '''
71 Serialize RDF/XML DOAP as N3 syntax
72
73 @param doap_xml: DOAP in RDF/XML serialization
74 @type doap_xml: string
75
76 @rtype: unicode
77 @return: DOAP in Notation 3
78 '''
79 if hasattr(self, 'options') and hasattr(self.options, 'no_color'):
80 color = not self.options.no_color
81 return get_n3(doap_xml, color)
82
84 """Add plugin's options to doapfiend's opt parser"""
85 output.add_option('-n', '--%s' % self.name,
86 action='store_true',
87 dest=self.enable_opt,
88 help='Output DOAP as Notation 3')
89 return parser, output, search
90