def import_md( url, modules=None, index_offset=0, force_tagname=None ):
	assert modules is not None
	doc = []
	code = []
	code_links = []
	code_idirs = []
	code_defines = []
	lang = False
	in_code = False
	index = 0
	prevline = None
	tag = force_tagname or None
	fences = 0
	base_path, markdown_name = os.path.split(url)
	#data = open(url, 'rb').read().decode('utf-8')
	import codecs
	data = codecs.open(url, 'r', 'utf-8').read()

	for line in data.splitlines():
		if line.startswith('* @link:'):
			code_links.append( os.path.expanduser(line.split(':')[-1]) )
		elif line.startswith('* @include:'):
			code_idirs.append( os.path.expanduser(line.split(':')[-1]) )
		elif line.startswith('* @define:'):
			code_defines.extend( line.split(':')[-1].strip().split() )
		# Start or end of a code block.
		elif line.strip().startswith('```'):
			fences += 1
			# End of a code block.
			if in_code:
				if lang:
					if lang=='python' and 'from rusthon import *' in code:
						rusthonpy = []
						for rln in open(__file__, 'rb').read().splitlines():
							if rln == 'if __name__ == "__main__":':
								break
							else:
								rusthonpy.append(rln)
						rusthonpy = '\n'.join(rusthonpy)
						utfheader = u'# -*- coding: utf-8 -*-\n'
						code.insert(0, utfheader + BOOTSTRAPED + '\n' + rusthonpy.decode('utf-8'))
						code.pop(code.index('from rusthon import *'))

					p, n = os.path.split(url)
					mod = {
						'path':p, 
						'markdown':url, 
						'code':'\n'.join(code), 
						'index':index+index_offset, 
						'tag':tag,
						'links':code_links,
						'include-dirs':code_idirs,
						'defines':code_defines,
					}
					if tag and '.' in tag:
						ext = tag.split('.')[-1].lower()
						#if ext in 'xml html js css py c cs h cpp hpp rust go java json'.split():
						mod['name'] = tag

					modules[ lang ].append( mod )
				in_code = False
				code = []
				code_links = []
				code_idirs = []
				index += 1
			# Start of a code block.
			else:
				in_code = True
				if prevline and prevline.strip().startswith('@'):
					tag = prevline.strip()[1:]
				else:
					tag = None

				lang = line.strip().split('```')[-1]
		# The middle of a code block.
		elif in_code:
			code.append(line)
		else:
			## import submarkdown file ##
			if line.startswith('* ') and '@import' in line and line.count('[')==1 and line.count(']')==1 and line.count('(')==1 and line.count(')')==1:
				submarkdown = line.split('(')[-1].split(')')[0].strip()
				subpath = os.path.join(base_path, submarkdown)
				if not os.path.isfile(subpath):
					raise RuntimeError('error: can not find markdown file: '+subpath)
				#print 'importing submarkdown'
				#print subpath
				index += import_md( subpath, modules, index_offset=index )

			doc.append(line)

		prevline = line

	modules['markdown'] += '\n'.join(doc)
	if fences % 2:
		raise SyntaxError('invalid markdown - unclosed tripple back quote fence in: %s' %url)


	return index
dataset/ETHPy150Open rusthon/Rusthon/rusthon.py/import_md/original