<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _expand_source(source, dname, fmt):
 """Expands source text to include headers, footers, and expands Multimarkdown transclusion directives. Keyword arguments: source -- string containing the Multimarkdown text to expand dname -- directory name to use as the base directory for transclusion references fmt -- format flag indicating which format to use to convert transclusion statements """

    _MMD_LIB.g_string_new.restype = ctypes.POINTER(GString)
    _MMD_LIB.g_string_new.argtypes = [ctypes.c_char_p]
    src = source.encode('utf-8')
    gstr = _MMD_LIB.g_string_new(src)

    _MMD_LIB.prepend_mmd_header(gstr)
    _MMD_LIB.append_mmd_footer(gstr)

    manif = _MMD_LIB.g_string_new(b"")
    _MMD_LIB.transclude_source.argtypes = [ctypes.POINTER(GString), ctypes.c_char_p,
                                           ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(GString)]
    _MMD_LIB.transclude_source(gstr, dname.encode('utf-8'), None, fmt, manif)
    manifest_txt = manif.contents.str
    full_txt = gstr.contents.str
    _MMD_LIB.g_string_free(manif, True)
    _MMD_LIB.g_string_free(gstr, True)

    manifest_txt = [ii for ii in manifest_txt.decode('utf-8').split('\n') if ii]
    return full_txt.decode('utf-8'), manifest_txt