dictdoc: factor out complex_to_doc logic to a separate function
get rid of magic numbers and other code refactorings.