# Path: academicmarkdown/_YAMLParser.py
# class YAMLParser(BaseParser):
# 	
# 	def __init__(self, _object, required=[], verbose=False):
# 		
# 		"""
# 		Constructor.
# 		
# 		Arguments:
# 		_object		--	Indicates which objects should be parsed.
# 		
# 		Keyword arguments:
# 		required	--	A list of required options in the YAML block.
# 						(default=[])
# 		verbose		--	Indicates whether verbose output should be generated.
# 						(default=False)
# 		"""
# 		
# 		self._object = _object
# 		self.required = required
# 		super(YAMLParser, self).__init__(verbose=verbose)
# 	
# 	def parse(self, md):
# 		
# 		"""See BaseParser.parse()."""
# 		
# 		for r in re.finditer(u'%--(.*?)--%', md, re.M|re.S):
# 			try:
# 				d = yaml.load(r.groups()[0])
# 			except:
# 				self.msg(u'Invalid YAML block: %s' % r.groups()[0])
# 				continue
# 			if not isinstance(d, dict):
# 				continue
# 			obj = list(d.keys())[0]
# 			if obj.lower() != self._object:
# 				continue
# 			keys = d[obj]
# 			for key in self.required:
# 				if key not in keys:
# 					raise Exception( \
# 						u'"%s" is a required option for %s objects' % (key, \
# 						self._object))
# 			md = self.parseObject(md, r.group(), keys)
# 		return md
# 	
# 	def parseObject(self, md, _yaml, d):
# 		
# 		"""
# 		Parses a specific YAML object found in a Markdown text.
# 		
# 		Arguments:
# 		md		--	The full markdown text.
# 		yaml	--	The yaml block.
# 		d		--	The yaml block already parsed into a dictionary.
# 		
# 		Returns:
# 		The parsed Markdown text.
# 		"""
# 		
# 		return md


from academicmarkdown import YAMLParser
from academicmarkdown.py3compat import *
			from io import StringIO # Py 3
			from StringIO import StringIO # Py 2
import subprocess
import shlex
		import sys
			img = True
		self.msg(u'Python: %s' % d)
		buffer = StringIO()
		sys.stdout = buffer
		if safe_str(d).startswith('# should-raise\n'):
			code = code.replace('# should-raise\n', '')
			try:
				exec('#-*- coding:utf-8 -*-\n%s' % safe_str(d), _globals)
			except Exception as e:
				print('{0}: {1}'.format(e.__class__.__name__, e))
				# import traceback
				# traceback.print_exc(file=buffer, limit=1)
			else:
				raise ValueError('Code should raise exception but didn\t')
		else:
			exec('#-*- coding:utf-8 -*-\n%s' % safe_str(d), _globals)
		sys.stdout = sys.__stdout__
		output = buffer.getvalue()
		self.msg(u'Returns: %s' % output)
		s = u"""
~~~ .python
%s
~~~
""" % safe_str(code)

		if output:
			s += """
__Output:__

~~~ .text

# Path: academicmarkdown/_YAMLParser.py
# class YAMLParser(BaseParser):
# 	
# 	def __init__(self, _object, required=[], verbose=False):
# 		
# 		"""
# 		Constructor.
# 		
# 		Arguments:
# 		_object		--	Indicates which objects should be parsed.
# 		
# 		Keyword arguments:
# 		required	--	A list of required options in the YAML block.
# 						(default=[])
# 		verbose		--	Indicates whether verbose output should be generated.
# 						(default=False)
# 		"""
# 		
# 		self._object = _object
# 		self.required = required
# 		super(YAMLParser, self).__init__(verbose=verbose)
# 	
# 	def parse(self, md):
# 		
# 		"""See BaseParser.parse()."""
# 		
# 		for r in re.finditer(u'%--(.*?)--%', md, re.M|re.S):
# 			try:
# 				d = yaml.load(r.groups()[0])
# 			except:
# 				self.msg(u'Invalid YAML block: %s' % r.groups()[0])
# 				continue
# 			if not isinstance(d, dict):
# 				continue
# 			obj = list(d.keys())[0]
# 			if obj.lower() != self._object:
# 				continue
# 			keys = d[obj]
# 			for key in self.required:
# 				if key not in keys:
# 					raise Exception( \
# 						u'"%s" is a required option for %s objects' % (key, \
# 						self._object))
# 			md = self.parseObject(md, r.group(), keys)
# 		return md
# 	
# 	def parseObject(self, md, _yaml, d):
# 		
# 		"""
# 		Parses a specific YAML object found in a Markdown text.
# 		
# 		Arguments:
# 		md		--	The full markdown text.
# 		yaml	--	The yaml block.
# 		d		--	The yaml block already parsed into a dictionary.
# 		
# 		Returns:
# 		The parsed Markdown text.
# 		"""
# 		
# 		return md


# Path: academicmarkdown/_PythonParser.py
from academicmarkdown import YAMLParser
from academicmarkdown.py3compat import *
			from io import StringIO # Py 3
			from StringIO import StringIO # Py 2
import subprocess
import shlex
		import sys

			img = True
		self.msg(u'Python: %s' % d)
		buffer = StringIO()
		sys.stdout = buffer
		if safe_str(d).startswith('# should-raise\n'):
			code = code.replace('# should-raise\n', '')
			try:
				exec('#-*- coding:utf-8 -*-\n%s' % safe_str(d), _globals)
			except Exception as e:
				print('{0}: {1}'.format(e.__class__.__name__, e))
				# import traceback
				# traceback.print_exc(file=buffer, limit=1)
			else:
				raise ValueError('Code should raise exception but didn\t')
		else:
			exec('#-*- coding:utf-8 -*-\n%s' % safe_str(d), _globals)
		sys.stdout = sys.__stdout__
		output = buffer.getvalue()
		self.msg(u'Returns: %s' % output)
		s = u"""
~~~ .python
%s
~~~
""" % safe_str(code)

		if output:
			s += """
__Output:__

~~~ .text

%s